Summary: in this tutorial, you will learn about the PHP __call()
magic method and how to use it to wrap existing functions.
Introduction to the PHP __call magic method #
When you call a method of an object, if the method does not exist or is private, PHP will call the __call()
method automatically.
The following shows the syntax of the __call()
method:
public __call ( string $name , array $arguments ) : mixed
Code language: PHP (php)
The __call()
method accepts two arguments:
$name
is the name of the method that you call on the object.$arguments
is an array of arguments you pass to to the method call.
The __call()
method is useful when you want to create a wrapper class that wraps existing API.
PHP __call() magic method example #
Suppose that you want to develop the Str
class that wraps existing string functions such as strlen()
, strtoupp()
and strtolower()
.
Typically, you can define these methods explicitly like length, upper, lower, … Doing so would take time. Instead, you can use utilize the __call()
magic method to make the code more concise.
The following defines the Str class that uses the __call()
magic method:
<?php
class Str
{
private $s = '';
private $functions = [
'length' => 'strlen',
'upper' => 'strtoupper',
'lower' => 'strtolower'
'substring' => 'substr'
// map more method to functions
];
public function __construct(string $s)
{
$this->s = $s;
}
public function __call($method, $args)
{
if (!in_array($method, array_keys($this->functions))) {
throw new BadMethodCallException();
}
array_unshift($args, $this->s);
return call_user_func_array($this->functions[$method], $args);
}
}
Code language: PHP (php)
How it works.
The $functions
property store the mapping between the methods and built-in string functions.
For example, if you call the length()
method on a string object, the __call()
method will call the strlen()
function.
When you call a method on an instance of the Str
class and that method doesn’t exist e.g., length()
, PHP will invoke the __call()
method.
The __call()
method will raise a BadMethodCallException
if the method is not supported. Otherwise, it’ll add the string to the argument list before calling the corresponding function.
The following shows how to uses the Str
class:
<?php
require 'Str.php';
$s = new Str('Hello, World!');
echo $s->upper() . '<br>'; // HELLO, WORLD!
echo $s->lower() . '<br>'; // hello, world!
echo $s->length() . '<br>'; // 13
Code language: PHP (php)
Output:
HELLO, WORLD!
hello, world!
13
Code language: PHP (php)
In this example, the $args
of the __call()
method is an empty string.
The following example shows how to call the substring method:
<?php
require 'Str.php';
$s = new Str('phptutorial.net');
echo $s->substring(0,11);
Code language: PHP (php)
Output:
phptutorial
Code language: PHP (php)
In this example, the $args
argument of the __call()
method is [0,11]
.
Summary #
- The
__call()
method is invoked automatically when a nonexisting method or an inaccessible method is called. - Use the
__call()
method to wrap existing API into a class.