PHP: Increment the variable each time you run the function - php

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.

Related

Optimize accesor function laravel

I have accesor, this function return position order for records of my model:
public function getPositionAttribute()
{
$n = $this->count();
for ($i = 0; $i < $n; $i++) {
$s = $this->get(['id'])->toArray()[$i]['id'];
if ($s == $this->id) {
return $i + 1;
}
}
}
How I can optimize this code? I think, that he works very very slow, because I going through a lot of records. May be do this with map function? I tryied, but map return collection, but not a number. My code with map:
public function getPositionAttribute()
{
$this->get(['id'])->map(function($item, $key) {
if ($item->id == $this->id) {
return $key + 1;
}
});
}
Map function return: #[1,NULL], #[NULL,2] e.t.c.
How I can correctly do my accesor function?
If i understood what your code does correctly, I'd probably do it like this:
public function getPositionAttribute()
{
$collection = $this->all()->pluck('id');
$position = $collection->search($this->id);
return $position ? ++$position : 0;
// 0 means the id could not be found
// You could easily swap that out with null or false.
}
Could you use the count of records having an earlier assigned primary key? For example:
public function getPositionAttribute()
{
return static::query()->where($this->getKeyName(), '<=', $this->getKey())->count() + 1;
}
Please try this and let me know if it works...
public function getPositionAttribute()
{
foreach($this->all() as $key => $item) {
if($this->id == item) return $key;
};
}
remember the collection starts in 0 maybe you need to add 1 before return $key.

Call to undefined function factorial()

my code is simple. but it gives me the error above that functorial function inside the function is undefined . why ? thanks?
<?php
class fact
{
public function factorial($number) {
if ($number < 2) {
return 1;
} else {
return ($number * factorial($number-1));
}
}
}
$obj = new fact();
var_dump($obj->factorial(6));
?>
Referencing factorial will look for a global function of that name. But you've written it as a method, so it must be called specifically on the object:
return ($number * $this->factorial($number-1));
$this-> references the object instance it's being called within.
The recursion call need to be prefixed with $this as follows:
<?php
class fact
{
public function factorial($number) {
if ($number < 2) {
return 1;
} else {
return ($number * $this->factorial($number-1));
}
}
}
$obj = new fact();
var_dump($obj->factorial(6));

How can i change the value of $number here

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;
}

php function return empty value when param set to a variable

I've been wondering why my function return to an empty value(NULL) when putting variable parameter on it..
function: (return empty value)
private static function typeObjectId($type_param) {
...
...
(some code)
for($i = 0; $i < count($results); $i++) {
if($results[$i]->type == $type_param) {
$some_var = $results[$i]->objectId;
return $some_var;
}
function: (return a value)
private static function typeObjectId() {
...
...
(some code)
for($i = 0; $i < count($results); $i++) {
if($results[$i]->type == 'Lorem') {
$some_var = $results[$i]->objectId;
return $some_var;
}
caller: (return empty value)
public function functionName() {
$var_param = 'Lorem';
$var = self::typeObjectId($var_param);
return $var;
}
caller: (Return a value)
public function createPostImage() {
$var = self::typeObjectId();
return $var;
}
P.S
The value that return is coming from a cloud storage..
Any help would be appreciated.
Thanks.

Why the Functions aren't defined ? PHP

What do I have to do so that I can use the standard php functions without creating an instance of a Math_functions Class ?
<?php
class Math_functions {
public static function evenNumber($number) {
return !($number & 1);
}
public static function natual_sum($n) {
while ($n) {
if (evenNumber($n)) {
$sum = $sum + $n;
}
$n--;
}
return $sum;
}
}
echo natual_sum(4);
?>
This is a static function. You have to access it using class name. Use like this
Math_functions::natual_sum(4);
Just put the functions into a PHP File.
You don't NEED the class
Example:
<?php
function evenNumber($number) {
return !($number & 1);
}
function natual_sum($n) {
while ($n) {
if (evenNumber($n)) {
$sum = $sum + $n;
}
$n--;
}
return $sum;
}
echo natual_sum(4);
?>

Categories