How to dynamically call a class method in PHP?

How can I dynamically call a class method in PHP? The class method is not static. It seems that

call_user_func(...) 

only works with static functions?

Thank.

+50
php callback
Nov 07 '08 at 18:50
source share
9 answers

It works both ways - you need to use the correct syntax

 // Non static call call_user_func( array( $obj, 'method' ) ); // Static calls call_user_func( array( 'ClassName', 'method' ) ); call_user_func( 'ClassName::method' ); // (As of PHP 5.2.3) 
+89
Nov 07 '08 at 18:53
source share

Option 1

 // invoke an instance method $instance = new Instance(); $instanceMethod = 'bar'; $instance->$instanceMethod(); // invoke a static method $class = 'NameOfTheClass'; $staticMethod = 'blah'; $class::$staticMethod(); 

Option 2

 // invoke an instance method $instance = new Instance(); call_user_func( array( $instance, 'method' ) ); // invoke a static method $class = 'NameOfTheClass'; call_user_func( array( $class, 'nameOfStaticMethod' ) ); call_user_func( 'NameOfTheClass::nameOfStaticMethod' ); // (As of PHP 5.2.3) 

Option 1 is faster than option 2, so try using them if you don't know how many arguments you are going to pass to the method.




Edit: The previous editor did an excellent job cleaning up my answer, but removed the link to call_user_func_array, which is different from call_user_func.

PHP has

 mixed call_user_func ( callable $callback [, mixed $parameter [, mixed $... ]] ) 

http://php.net/manual/en/function.call-user-func.php

AND

 mixed call_user_func_array ( callable $callback , array $param_arr ) 

http://php.net/manual/en/function.call-user-func-array.php

Using call_user_func_array is several orders of magnitude slower than using any of the above options.

+23
Nov 07 '08 at 22:59
source share

Do you mean this?

 <?php class A { function test() { print 'test'; } } $function = 'test'; // method 1 A::$function(); // method 2 $a = new A; $a->$function(); ?> 
+16
Nov 07 '08 at 18:54
source share
 call_user_func(array($object, 'methodName')); 

See the php callback documentation for more information.

+2
Nov 07 '08 at 18:53
source share

EDIT: I just developed what you were trying to ask ... but how good .. will leave my comments anyway. You can replace class and method names with variables if you want .. (but you're crazy) - nick




To call a function from a class, you can do this in one of two ways ...

Or you can instantiate the class and then call it. eg:.

 $bla = new Blahh_class(); $bla->do_something(); 

or ... you can call the function statically, i.e. without a class instance. eg:.

 Blahh_class::do_something() 

of course you need to declare your function is static:

 class Blahh_class { public static function do_something(){ echo 'I am doing something'; } } 

If the class is not defined as static, then you must instantiate the object .. (therefore, the object needs a constructor) for example :.

 class Blahh_class { $some_value; public function __construct($data) { $this->$some_value = $data; } public function do_something() { echo $this->some_value; } } 

It is important to remember that static functions of a class cannot use $this , since there is no instance of the class. (This is one of the reasons they go much faster.)

+1
Nov 10 '08 at 2:27
source share

The best way to find

 call_user_func_array(array(__NAMESPACE__ .'\Foo', 'test'), array('Philip')); 

It works like a charm!

0
Jun 04 2018-12-14T00:
source share
 Class Foo{ public function show(){ echo 'I am in Foo Class show method'; } } call_user_func(array('Foo', 'show')); $classname = new Foo; call_user_func(array($classname, 'show')); call_user_func($classname .'::show'); // As of 5.2.3 $foo = new Foo(); call_user_func(array($foo, 'show')); 
0
Jul 22 '13 at 21:37
source share

This may be useful as a substitute.

 class ReferenceContainer { function __construct(CallbackContainer $callbackContainer) { //Alternatively you can have no parameters in this constructor and create a new instance of CallbackContainer and invoke the callback in the same manner //var_dump($this->callbackContainer); $data = 'This is how you parse a class by reference'; $callbackContainer->myCallback($data); } } class CallbackContainer { function __construct() {} function myCallback($data) { echo $data."\n"; } } $callbackContainer = new CallbackContainer(); $doItContainer = new ReferenceContainer($callbackContainer); 
0
May 03 '16 at 5:12
source share

Starting with PHP7, you use the array method:

 // Static call only [TestClass::class, $methodName](...$args); // Dynamic call, static or non-static doesn't matter $instance = new TestClass; [$instance, $methodName](...$args); 

Just replace the class name with TestClass , the method name with $methodName and the method arguments with ...$args . Note that in the latter case it does not matter that the method is static or non-static; PHP will call this automatically.

0
Jul 17 '19 at 12:35
source share



All Articles