Assigning a static function to a variable in PHP

I would like to assign a static function to a variable so that it can be sent as a parameter. For instance:

class Foo{ private static function privateStaticFunction($arg1,$arg2){ //compute stuff on the args } public static function publicStaticFunction($foo,$bar){ //works $var = function(){ //do stuff }; //also works $var = function($someArg,$someArg2){ //do stuff }; //Fatal error: Undefined class constant 'privateStaticFunction' $var = self::privateStaticMethod; //same error $var = Foo::privateStaticFunction; //compiles, but errors when I try to run $var() somewhere else, as expected //Fatal error: Call to private method Foo::privateStaticMethod() from context '' $var = function(){ return Foo::privateStaticMethod(); }; } } 

I tried a few more options, but none of them worked.

I don’t even expect such a functional hack for working with PHP, but hey, who knows?

Is it possible to do this in PHP or will I need to come up with some kind of hack using eval ?

PS: LawnGnome in ## php mentioned something that you can do what I want to use array('Foo','privateStaticMethod') , but I did not understand what he had in mind, and I did not click on him further while he looked busy.

+4
source share
3 answers

You want to use call_user_func with an array argument.

 $var = array('Foo','privateStaticMethod'); call_user_func($var); 

Note that the "private" qualifier of this method will only make it callable this way in the Foo class.

call_user_func takes callable as the first argument. Note that with PHP 5.2.3 you can avoid the notation of an array in your case.

Static class methods can also be passed without instantiating an object of this class by passing the class name instead of the object to index 0. Starting with PHP 5.2.3, you can also pass' ClassName :: MethodName.

+3
source

As a note, you should set your static variable as follows:

 static $privateStaticMethod; $var = self::$privateStaticMethod; 

Leaving $ when using self:: will try to access the CONSTANT class, not the variable.

In fact, I think you meant this line:

 //Fatal error: Undefined class constant 'privateStaticFunction' $var = self::privateStaticFunction("arga", "argb"); 

If you are on PHP 5.4, you can do the following in your publicStaticFunction :

 $var = function(){ return self::privateStaticFunction(); }; $var(); 

In PHP 5.4, PHP allows you to access class scope from lambda functions.

Also, have you looked at the ReflectionClass ?

To replace call_user_func_array() in a more OOP style, the following will be used:

 <?php $rc = new ReflectionClass('SomeClass'); $class = $rc->newInstanceArgs(array('foo', 'bar')); echo $class->doSomething(); 

You can write your own class to use ReflectionMethod , which implements ReflectionClass and use setAccessible() to allow access to protected and private methods.

+2
source

Here is what I did to solve my problem

 class test{ static function Unix($arr){return implode("/",$arr);} static function Windows($arr){return implode("\\",$arr);} function test(){ $slash = "Unix"; echo self::$slash(["path","to","a","folder"]); } } 
+1
source

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


All Articles