__callStatic() Method¶
__callStatic() is a magic method: it is called when a static method cannot be accessed, either because it doesn’t exist, or because its visibility prevents it.
That way, it is possible to create dynamically static methods, without hardcoding them.
The __callStatic() method receives the name of the target-method, and a list of argument.
The __callStatic() method has a related version for non-static methods, called __call().
<?php
class x {
private $values = ['a' => 1,
'b' => 2,
];
// Checks the dictionary, or return 0
function __call($name, $args) {
if (method_exists($this, $name)) {
return $this->$name(...$args);
}
return 0;
}
}
?>
See also Understand more magic methods: __callStatic().
Related : Magic Methods, __call() Method