All the ways to call a method in PHP
There are many ways to call a method in PHP. Method calling is a very common task, and, although the most common syntax is the overwhelming majority, there are some niche syntaxes for specific usages. And some good old syntaxes too.
Since they are scattered across the manual, we have gathered them here. All of them.
The function classic way
Hardcoded, directly in the source. This is the syntax that everyone learn first.
Note that the function name may be aliased, or imported from another namespace with the use
expression. The alias only affects the call of the function, and not the function itself.
With a dynamic name
A dynamic name means that the function’s name is stored in a string.
The string is usually stored in a variable or similar, and it is used in place of the function name: PHP reads the string in the variable, and the correct function is called.
Always use the fully qualified name in the string, as local names are not solved. This usually leads to surprises when inside a namespace.
As this must be done even for locally defined functions, __NAMESPACE__
comes to the rescue.
With a string
The previous entry has one edge case: it is possible to use a string as the name of the function and call it directly: indeed, there is no need to use a variable.
Compared to the classic way, the string looses the aliasing features, provided by the use
expression.
On the other hand, it is also possible to use the double quotes strings, and put some variables in it. This is a dynamic syntax.
With a closure
Instead of calling the function by referencing its absolute name, PHP creates closures from any function or method. Since PHP 8.2, the ...
operator, used as only argument in the function, returns a closure that represents the function. Then, that closure may be used to actually call the original function.
Also, for fun, there can be many closures creation before, finally, calling it. This is useless syntax, but rather funny.
With a call to call_user_func()
Why not call a function by calling … another function? Use the native call_user_func
or call_user_func_array
to call any function or method. The first parameter is the name of the function, and the same rules we have already seen apply : make the name of the function fully qualified.
call_user_func
and call_user_func_array
are useful when both the function and the parameters are dynamic.
Calling methods with the array syntax
The previous entry is also useful to call methods. Except that methods don’t have the same string syntax, so we need to introduce the array syntax.
Compared to calling a function, a method requires two parts: the object or the class, and the method name. And, of course, the parameters.
PHP features a special syntax, based on arrays: to call a method on an object, create an array of two elements, with the object and the method name. Then, call it like that.
Like for the strings, it is possible to call directly on the literal array, as long as it contains the correct information.
Calling methods with the array syntax and the relative class names
The relative class names are self
, static
and parent
. They are not class names per se, as it is not allowed to create classes with such names. But they to represent actual classes, which vary from definition to definition.
The relative class names are collected at definition time. When the array is moved around the program, it will always refer to the method that was targeted initially, not at the point of execution.
Also, note that PHP doesn’t allow anymore the usage of any of the relative classnames in strings, since PHP 8.2. The code below produces a string error message: Class "static" not found
Calling static methods
The previous syntax is also available for static methods: then, the first element of the array may be an object or the name of the class. PHP picks up anything that is useful to make the call succeed.
Calling methods with a string
Calling methods with a string is possible, as long as the method is static. Then, the string should be written as class::method
, and target a static method.
That approach is not possible with objects, which are not compatible with a string.
Calling the anonymous method
One more way to call a method is to call it directly from an object.
That is the purpose of the __invoke
method: it is a magic method, which is called automatically when the object itself is used as a function name. Like this:
Note that __invoke
is not anonymous, as … it has a name. On the other hand, calling $object()
means that there is no need for the name of the method to be called, so this is anonymous.
The Reflection call
The reflection API allows for PHP code introspection, reading descriptive features of any defined structures. It may be lesser known that such structures are still executable, and this applies to the functions and methods.
In the case of methods and functions, the reflectionFunction class has an invoke() method and a invokeArgs() method.
Thanks to Benoit Viguier for the reminder.
The evergreen eval() call
One always have remember old PHP features, and eval() is definitely one of them. Since eval() requires some PHP code in a string, it is another way to call a method or a function or anything really.
Thanks to Anna Filina for the reminder.
Calling methods the classic way
Finally, the classic way to call methods: this is achieved with the object operator ->
and the static operator ::
.
foo(); // classic method call $x::bar(); // static method call on object X::bar(); // static method call on class name ?>
That’s all folks!
We have covered no less than eleven (11!!) different ways to call PHP methods and functions. The first and last are the most commonly used, with good reasons. The other ones covers niche usages, such as dynamic method call, or dynamic parameter collections.