PHP Magic methods _call() and _callStatic()

Andrei Birta
2 min readJan 3, 2023

--

Foto: pexels.com

In PHP, magical methods are special methods that are automatically called by PHP execution under certain circumstances. These methods are set using double underlining and are used to implement some features in your classes.

The __call() magic method:

Is called when an unspecified method is called onto an object. This can be helpful if you want to create a “fallback” method that can manage calls to any method, even if it has not been set.

For example, consider the following class:

class ExampleClass {
public function __call($name, $arguments) {
echo "Method $name is missing!\n";
}
}

If you try to call an undefined method on an instance of this class, the __call() method will be called instead.

For example:

$obj = new ExampleClass();
$obj->exampleMethod(); // Outputs: "Method exampleMethod is missing!"

The __call() method takes two arguments: $name and $arguments. $name s the name of the unspecified method that was invoked, and $arguments is an array of arguments passed to the method.

You can use the __call() method to create a "fallback" method that can handle calls to any method, even if it hasn't been defined. For example, you can use it to create a “dynamic” method which can perform different tasks based on the name of the named method.

class ExampleClass{
public function __call($name, $arguments) {
if ($name == 'doThis') {
// Do something here
} else {
echo "Method $name is missing!\n";
}
}
}

$obj = new ExampleClass();
$obj->doThis(); // Will execute the "Do something here" code
$obj->doAnotherMethod(); // Outputs: "Method doAnotherMethod is missing!"

The __callStatic() magic method:

Works in a similar way as __call(), but is called when an undefined static method is called on a class.

For example:

class ExampleClass{
public static function __callStatic($name, $arguments) {
echo "Static method $name is missing!\n";
}
}

MyClass::exampleMethod(); // Outputs: "Static method exampleMethod is missing!"

As with __call(), the __callStatic() method takes two arguments: $name and $arguments. $name is the name of the undefined static method that was appended, and $arguments is an array of arguments sent to the method.

You can use the __callStatic() method to create a "fallback" static method that can manage calls to any static method, although it has not been defined.

For example, you can use it to create a static “dynamic” method which can execute different tasks according to the name of the method called.

class ExampleClass{
public static function __callStatic($name, $arguments) {
if ($name == 'doThis') {
// Do something here
} else {
echo "Static method $name is missing!\n";
}
}
}

MyClass::doThis(); // Will execute the "Do something here" code
MyClass::doAnotherMethod(); // Outputs: "Static method doAnotherMethod is missing!"

Get more valuable insights and tips by following me!
Also check my list of articles about the PHP or different type of architecture.

--

--