static __call

As everybody knows, one of very nice OO features PHP 5 has is – if method that is not defined is called on an object of a class, the class could define catch-all method named __call and thus route this method call in any way the developer wants, transparent to the user. This allows very flexible way of defining interfaces between classes – even between entities that their interface might be not known to the developer of the class, such as SOAP services. Very useful indeed.

However, we can not do this on a class itself – we couldn’t define static __call and have it route class (static) method calls the same way regular __call routes the object method calls. I wonder maybe we should have it. Along with all other __methods for overloading stuff, of course. We couldn’t probably name it __call since we already have one call but something like __scall could work.

11 thoughts on “static __call

  1. I just googled “php __call static” too see if it was possible.

    wanted to write something like

    Service::message_send($level, $text, $code);

    but have to stick to

    Service::call($service, $method, array($level, $send, $code));

    I don’t want to implement the dynamic __call() because it messes to much with development: a typo no longer raises a fatal error… but I wouldn’t mind overloading the static calls to this class, no, no.

  2. Actually I recently saw a patch implementing it. don’t remember if it’s committed already but if it wasn’t there’s a good chance it would.

  3. Read in the PHP doc :

    “In fact static method calls are resolved at compile time”

    That’s why it’s not available, and probably will never be.

  4. I wouldn’t consider __call to be a “one of very nice OO features PHP 5”. It is really just a tacked on error handler (like __get/__set). I would be nice if they actually would implement some language level improvements in PHP’s OO, but that will never happen. I can see why there is such a clamor for namespaces, with the demand in PHP for hiding procedural programming methods in fancy constructs like you want to do here.

  5. I created a basic implementation of the active record design pattern for a talk I did at the start of the month. With a static version of __call() I would have been able to do a much cleaner $user = User::findByUsername('fred'); instead of $user = new User(); $user = $user->findByUsername('fred') or $user = User::find('username', 'fred');. I’m sure no one is listening but I made a call for it a few days ago.

  6. Maybe we could just route them through the regular __call? It’s up to the implementor of __call to know that it was the static one that was being called, and maybe you could even test for the presence of $this to see if it was static. This way, you also cover none-static methods that were accidentally called statically.

  7. Hmm, maybe too close to call … anyway, i am not a fan of such shortcuts anyway …
    But as wwe say in bavaria: was der Bauer ned kennt des frisst er ned. (What the farmer doees not know he doeesnt eat)

Comments are closed.