In PHP there are various ways in which you can dynamically invoke a static or non-static method. To learn about them, let's suppose we have the following variables; one referencing a class name, another an instantiated object, and a static and non-static method name:
$className = ClassName::class; $obj = new ClassName(); $nonStaticMethod = 'nonStaticMethod'; $staticMethod = 'staticMethod';
To dynamically invoke the methods you can do any of the following:
Using the Variable Function Syntax
You can use the variable function syntax with class methods in the following ways:
Dynamically Invoking a Non-Static Method:
You can use the method name variable directly on the object instance variable similar to how you normally would without a variable method name. For example:
// PHP 5.5+ $obj->$nonStaticMethod(); // or $obj->{$nonStaticMethod}();
Dynamically Invoking a Static Method:
If you have both, the class name (or the object instance) and the method name as a variable, then you could dynamically invoke the static method in the following way:
// PHP 5.5+ $className::$staticMethod();
// PHP 5.5+ $obj::$staticMethod();
If only the class name or object instance exists as a variable, you could directly write the method name in a single, combined string (which you could then invoke as a callable
) like so:
// PHP 7+ $callable = "{$className}::staticMethod"; $callable();
You could also inline this callable string in the following way:
("{$className}::staticMethod")();
Since the callable string syntax returns a callable
, you could also pass it as the first argument to either call_user_func()
or call_user_func_array()
function, for example, like so:
$callable = $obj::$staticMethod(); // or // $callable = $className::$staticMethod(); call_user_func($callable, $arg1, $arg2, ...); call_user_func_array($callable, [$arg1, $arg2, ...]);
Using the Callable Array Syntax
You can use the callable array syntax, where you specify the object instance or class name at index 0
and the method name at index 1
(regardless of whether it's a static method or a non-static one). For example:
// PHP 5.5+ $callable = [$obj, $nonStaticMethod]; $callable();
// PHP 5.5+ $callable = [$obj, $staticMethod]; $callable();
// PHP 5.5+ $callable = [$className, $staticMethod]; $callable();
You could also inline the whole call in the following way:
([$obj, $nonStaticMethod])(); ([$obj, $staticMethod])(); // ...
Since the callable array syntax returns a callable
, you could also pass it as the first argument to either call_user_func()
or call_user_func_array()
function, for example, like so:
$callable = [$obj, $nonStaticMethod]; call_user_func($callable, $arg1, $arg2, ...); call_user_func_array($callable, [$arg1, $arg2, ...]);
It works the same way for both static and non-static methods.
This post was published (and was last revised ) by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.