I want to create a class. Each time it will be called it will increase the value of $number by 1. When it will reach 7, it should return a message "Maximum limit reached". Where to define the $number and how to store the new value in it.
class addClass
{
public $number = 0;
public static function addOne($number)
{
$number = $number++;
if ($number == 7) {
return 'This is 7';
}
}
}`
I think this is what you are looking for based on your description:
class MyNumber {
private static $number = 0;
public static function addOne() {
self::$number++;
if (self::$number === 7) {
return 'Maximum limit reached';
}
return self::$number;
}
}
$result = MyNumber::addOne();
$result = MyNumber::addOne();
$result = MyNumber::addOne();
$result = MyNumber::addOne();
$result = MyNumber::addOne();
$result = MyNumber::addOne();
$result = MyNumber::addOne();
First result is 1
Second result is 2
Third result is 3
Fourth result is 4
Fifth result is 5
Sixth result is 6
Seventh result is Maximum limit reached
You won't need to have $number in the addOne Function
There are two alternatives
If you don't want to keep $number as static then you can change addOne to a non-static method and access using $this->
class addClass
{
public $number = 0;
public function addOne()
{
$this->number = $this->number + 1;
if ($this->number == 7) {
return 'This is 7';
}
}
}
Or if you want addOne to be static then you can declare $number as static and access using self::
class addClass
{
private static $number = 0;
public static function addOne()
{
self::number = self::number + 1;
if (self::number == 7) {
return 'This is 7';
}
}
}
Please bear in mind:
1) The $number parameter in the addOne() method is taking precedence over the $number member in the addClass() parameter.
2) The sentence $number = $number++ is no affecting the variable $number at all, because it is first being assigned.
3) The addOne() method doesn't need to be static, unless is intended to be used without an instance of the class addClass.
4) Static variables only need to be initialize once, refer to the php manual for more information on the static keyword: http://php.net/manual/en/language.oop5.static.php
5) You cannot reference member variables inside a static method (e.g. using $this), because static methods have "class scope" and they are meant to be used without any instance of such class. On the other hand, non static methods require an instance of the class and they can reference members of the class by using $this.
6) Here's an example of how you can do this:
<?php
class addClass{
public function addOne($number) {
static $limit = 0;
if (!isset($limit)) {
$limit = $number;
}
if ($limit+1 == 7) {
return "Maximum limit reached";
} else {
$limit = $number+1;
}
}
}
$a = new addClass();
for($i = 0; $i< 7; $i++) {
echo $i+1, " => ", $a-> addOne($i), PHP_EOL;
}
Related
I have a function in my Helper Class that should increment the variable each time the function is called.
Here is my code:
<?php
class Helper
{
public static $count;
public static function voiceHelper($questionArray)
{
$count = self::$count;
// $count = 0;
if(count($questionArray) >= $count)
{
$count++;
return $count;
} else if($count > count($questionArray))
{
$count == 0;
return $count;
}
}
}
I expect that the count variable will increment each time the function is called but it still remains 1.
Try:
class Helper
{
public static $count;
public static function voiceHelper($questionArray)
{
// $count = 0;
if(count($questionArray) >= $count)
{
self::$count++;
return self::$count;
} else if($count > count($questionArray))
{
self::$count = 0;
return self::$count;
}
}
}
Looks like you are just incrementing the $count without adding it to the static count property. Therefore you will always get 1. Instead actually increment the static count property.
You have to use self::$count everywhere:
<?php
class Helper
{
public static $count;
public static function voiceHelper($questionArray)
{
if(count($questionArray) >= self::$count)
{
self::$count++;
return self::$count;
}
if(self::$count > count($questionArray))
{
self::$count = 0; // change == to = as it's assignment
return self::$count;
}
}
}
Output:- https://3v4l.org/EaEqA And https://3v4l.org/pto7m
Note:- You did increment in the $count without adding it to the static count property. That's why you always got 1.
why can't i assign a function to a variable within a class: e.g
class call {
public $number = function() {
return 3 * 2;
}
}
$num = new call();
$num->number // expecting output 6
Is it possible to assign a method (function) to a property (variable) so that the method can be called outside the class just as a property. e.g
class call {
public $number = $this->value();
private function value() {
return 3 * 2;
}
}
$num = new call();
echo $num->$number // expecting output 6;
Use __get() magic method that called when you trying to get value of inaccessible properties
class call {
public function __get($name) {
if ($name == 'number')
return $this->value();
}
private function value() {
return 3 * 2;
}
}
$num = new call();
echo $num->number;
// 6
<?php
class SimpleClass{
var $number1;
var $number2;
public function input_two_no($num1,$num2){
$this->number1=$num1;
$this->number2=$num2;
}
public function the_sum(){
$total = $number1+$number2;
return $total;
}
public function output_the_sum(){
echo $total;
}
$numbers = new SimpleClass;
$numbers->input_two_no(10,5);
$numbers->the_sum();
$numbers->output_the_sum();
}
?>
Please point out where i am going wrong in this.I am not getting the output yet.
There are several things you're doing wrong, such as:
Take this code block $numbers = new SimpleClass; ... $numbers->output_the_sum(); outside of the class.
See this statement inside the_sum() method,
$total = $number1+$number2;
You didn't declare any local variables named $number1 and $number2 inside the_sum() method. Instead, you should make use of the instance variables here.
See this statement in output_the_sum() method,
echo $total;
You didn't declare any local variable named $total inside output_the_sum() method. Instead, create an instance variable named $total and store the sum value in this instance variable. Later, you can display the total sum value using echo $this->total;.
So your code should be like this:
class SimpleClass{
var $number1;
var $number2;
var $total;
public function input_two_no($num1,$num2){
$this->number1=$num1;
$this->number2=$num2;
}
public function the_sum(){
$this->total = $this->number1+$this->number2;
}
public function output_the_sum(){
echo $this->total;
}
}
$numbers = new SimpleClass;
$numbers->input_two_no(10,5);
$numbers->the_sum();
$numbers->output_the_sum();
First off, you don't need the 2 Methods. input_two_no($num1,$num2) and output_the_sum(). You may, however, create a basic constructor method. Secondly, to access member-variables (Properties), you need to use php object-access notation (->). your class should have been as shown below. the sum method itself returns the sum of the 2 numbers passed as arguments.
class SimpleClass{
var $number1;
var $number2;
public function __construct($num1=null, $num2=null){
$this->number1 = $num1;
$this->number2 = $num2;
}
public function the_sum($number1=null, $number2=null){
if(!is_null($number1) && !is_null($number2)){
return null;
}
if( (!is_int($number1) || !is_float($number1) || !is_double($number1)) &&
(!is_int($number2) || !is_float($number2) || !is_double($number2))
){
return 'Numeric inputs only...';
}
$this->number1 = $number1;
$this->number2 = $number2;
return $this->number1 + $this->number2;
}
}
$numbers = new SimpleClass;
$output = $numbers->the_sum(10,5);
echo $output; //<== YIELDS 15
<?php
$count=0;
class My extends Thread
{
private $myid;
// ini_set('max_execution_time', 0);
//echo date("Y-m-d H:i:s")."<br/>";
public function __construct($id)
{
$this->myid = $id;
}
public function run()
{
for($t=0;$j+$t<=100;$t+=10){ //future buy
for($k=0;$j+$t+$k<=100;$k+=10){//future sell
for($l=1;$l<=14;$l++){ // strike
for($m=0;$j+$k+$m<=300;$m+=10){ //put buy
for($n=1;$n<=14;$n++){ // strike
for($o=0;$o<=300;$o+=10){ // call buy
for($p=1;$p<=14;$p++){ //strike
if($p==$l)
continue;
for($q=0;$q<=300;$q+=10){ // put sell
for($r=1;$r<=14;$r++){ // strike
if($r==$n)
continue;
for($s=0;$s<=300;$s+=10){ // call buy
$count ++;
}
}
}
}
}
}
}
}
}
}
}
}
echo date("Y-m-d H:i:s")."<br/>";
$mycalls = [];
for($i=0;$i<=100;$i+=10)
{
$mycalls[$i]= new My($i);
$mycalls[$i]->start();
$mycalls[$i]->join();
}
echo date("Y-m-d H:i:s")."<br/>";
echo "<br>";
echo $count;
?>
Call run method of My class. and it should be either return $count or echo $count.
Not sure I completely understand what you mean by "how to get output of count variable", but I see a number of problems there might occur.
1) $count is a global variable and not a class or a method variable, which menas that simply adding 1 to count in th run method will not do any changes in $count outside the class definition.
2) I don't see any place where run method that updates $count (despite the fact that it is outside the class) variable is called.
3) Even if run method is somewhere called you have to add a line global $count at the beginning of run method.
4) I would suggest you store the $count internally in the class and return it with a function, which would mean that the class looks something like this:
Class My extends Thread{
protected $count = 0;
public function run(){
//...
$this -> count++;
//...
}
public function getCount(){
return $this -> count();
}
}
And then get the count value from the class instance:
$myInstance = new My();
$myInstance -> run();
$someCount = $myInstance -> getCount();
Due to the fact that you are creating 101 instances of my, the code would look something like this:
$mycalls = [];
$count = 0;
for($i=0;$i<=100;$i+=10)
{
$mycalls[$i]= new My($i);
$mycalls[$i]->start();
$mycalls[$i]->join();
$count += $mycalls[$i] -> getCount();
}
echo $count;
I'm having hard time accomplishing one simple task. I have a method that would generate random number and depending on the outcome assign specific outcome to an array variable. What i want to do is get that array variable through instance method which would be called from the other class.
<?php
class MyClass
{
public $results = array(array());
public function simulated_games()
{
$game_series1=array(23,34,56);
$game_series2=array(31,42,67);
$iter_wins=array(array());
for($i=0; $i<10;$i++)
{
$random = rand(0,100);
for($b=0; $b<1;$b++)
{
if($random <= $game_series1[0])
{
$iter_wins[$i][$b]=3;
}
else if($random <= $game_series2[0]+$game_series2[1])
{
$iter_wins[$i][$b]=1;
}
}
}
$results=$iter_wins;
}
>here i create method just to return variable
public function get_simulated_games()
{
return $this->results;
}
}
<?php
$a= new MyClass();
$a->simulated_games();
$array = array();
>here is the issue, it return just 1, but supposed to return range numbers
$array=$a->get_simulated_games();
for($f=0; $f<sizeof($array);$f++)
{
for($g=0; $g<5;$g++)
{
echo $array[$f][$g];
}
echo '<br>';
}
?>
You have the error in results.
You modify interal function variable which is not set instead of class variable
change
$results=$iter_wins;
to
$this->results=$iter_wins;