Dynamic Class Method Invocation in PHP

How do I dynamically invoke a class method in PHP?

It works both ways - you need to use the right 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)

Dynamic class method invocation in PHP

There is more than one way to do that:

$this->{$methodName}($arg1, $arg2, $arg3);
$this->$methodName($arg1, $arg2, $arg3);
call_user_func_array(array($this, $methodName), array($arg1, $arg2, $arg3));

You may even use the reflection api http://php.net/manual/en/class.reflection.php

Dynamic class method name in php

Please use __callStatic or __call magic methods. http://php.net/manual/en/language.oop5.overloading.php#object.callstatic

public static function __callStatic($name, $arguments)
{
//$name is your type
}

PHP - Dynamically create methods in a class by an array

You can use the __call to create magic methods, like this :

class Foo {

private $methods;

public function __construct(array $methods) {
$this->methods = $methods;
}

public function __call($method, $arguments) {
if(isset($this->methods[$method])) {
return $this->methods[$method];
}
}
}

$array = array(
'aFunctionName' => 'My Value',
'anotherFunctionName' => 'My other value'
);

$foo = new Foo($array);
echo $foo->aFunctionName();

dynamic class method call chaining by array values

Put your $array['method_*'] into {} brackets:

$result = $my_class->{$array['method_1']}($array['method_1'][0], $array['method_1'][0])
->{$array['method_1']}($array['method_2'][0], $array['method_2'][0])
->{$array['method_n']}($array['method_n'][0], $array['method_n'][0]);

If I may notice, although PHP allows this, for code readability and reliability you should rethink about dynamic chain calls. It may save you few lines, but may raise some headaches later debugging

// Appendix:
IMO, better way of doing your logic is via foreach and call_user_func_array():

foreach ($array as $method => $args) {
call_user_func_array(array($my_class, $method), $args);
}

How do I dynamically invoke a class method in PHP?

It works both ways - you need to use the right 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)

How can I dynamically invoke all instance methods starting with a given prefix?

Edit: See the answer by localheinz below for a better method, using reflection. get_class_methods() will only return public methods.


Building off hsz's answer:

You can get the list of a class' methods using get_class_methods(). Then you can loop through the results, and call the method if it starts with "social_".

// Get the list of methods
$class_methods = get_class_methods("social_button");

// Loop through the list of method names
foreach ($class_methods as $method_name)
{
// Are the first 7 characters "social_"?
if (substr($method_name, 0, 7) == "social_")
{
// Call the method
$this->{$method_name}();
}
}

Dynamic static method call in PHP?

Use the call_user_func function:

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

Example:

call_user_func('myClassName_' . $language . '::myFunctionName');


Related Topics



Leave a reply



Submit