__callStatic in PHP 5.3

This is cool (and a feature I’ve been missing in past projects): being able to implement a __call method for static class methods.
Like this:

1
2
3
4
5
6
7
class Foo
{
	public static function __callStatic($method, $args) {
		echo "Method $method has been called!" .PHP_EOL;
		echo "With following arguments: " .implode(", ", $args) .PHP_EOL;
	}
}

So you can call

Foo::anyMethod('with', 'any', 'arguments');

and have a working static class method.

In this case, the method outputs

Method anyMethod has been called!
With following arguments: with, any, arguments

This is already implemented in the PHP 5.3 and PHP 6 development versions.
For more information on what __call (and the future __callStatic) magic methods do, see php.net Method overloading

0 Responses to “__callStatic in PHP 5.3”


  • No Comments

Leave a Reply