Returning a variable argument list method as a reference in PHP

I am trying to return a value from a method as a reference in PHP5.3. I may be mistaken in this, but I am bringing an older project to speed up work with some of the newer 5.3+ features.

Below is an example that I whipped to explain what was going on:

class Foo { static $foobar = 5; function &bar() { return self::$foobar; } } // Doesn't work //$test1 = &call_user_func_array(array("Foo","bar"),array()); // Doesn't work //$test1 = &call_user_func_array("Foo::bar",array()); // Doesn't work //$f = new Foo; $test1 = &call_user_func_array(array($f,"bar"),array()); // WORKS //$test1 = &Foo::bar(); //Doesn't work //$function = "Foo::bar"; //$test1 = &$function(); // WORKS $f = new Foo; $test1 = &$f->bar(); $test2 = Foo::bar(); var_dump($test1); var_dump($test2); $test1 = 10; echo "----------<br />"; var_dump($test1); var_dump($test2); var_dump(Foo::bar()); //returns 10 when working, 5 when not working 

The very latest Foo::bar() should return 10, since $test1 should be a reference to Foo::$foobar when everything works.

I understand that this example also uses some funky legacy PHP call Foo::bar , and the bar() method is not set as static, but can still be called via ::

Any help would be greatly appreciated since the only fix I have so far is simply setting switch in the argument list and calling the method directly based on how many arguments exist.

+4
source share
3 answers

It just sets $ test1 to $ foobar (which is 5)

 $test1 = &$f->bar(); 

It just overwrites the value contained in $ test1 with 10

 $test1 = 10; 

If you want to update the value in Foo, use

 $f->foobar = 10; 
0
source

Does it work in PHP 5.2.5: http://codepad.org/uMEIK210 (note 10 as the final result)?

I suppose you would like to see 10 three times.

For this (that $test2 also a reference to the class field) you need to specify & on both sides: function &bar() and $test2 =& Foo::bar();

See the docs :

Note. . Unlike passing parameters, here you should use & in both places - to indicate that you want to return by reference, not a copy, and indicate that the binding link, and not the usual destination, should be made for $ myValue.

So you just need to edit one line to get (possibly) 3 x 10:

 $test2 =& Foo::bar(); 

Final hint

Do not use PHP links

0
source

First of all, try declaring a static function. Also, the call should be a regular call. with the ampersand prefix, as already mentioned.

 class Foo { static $foobar = 5; public static function &bar() { return self::$foobar; } } 

Call:

 $test1 =& Foo::bar(); 

In addition, I see no real reason for referring to a static variable. A static variable is a variable that does not change the value between calls. This is basically a "global" var, enclosed in a namespace. You only need read access from outside the class, writing must be done internally, in accordance with the principle of encapsulation. No link needed, really ..

0
source

Source: https://habr.com/ru/post/1386547/


All Articles