__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 (sic) methods, without hardcoding them.
The __call() method receives the name of the target-method, and a list of argument.
The __call() method has a related version for static methods, called __callStatic.
<?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