views:

496

answers:

3

PHP5 has a "magic method" __call()that can be defined on any class that is invoked when an undefined method is called -- it is roughly equivalent to Ruby's method_missing or Perl's AUTOLOAD. Is it possible to do something like this in older versions of PHP?

A: 

I recall using it, and a little bit of googling suggests that

function __call($method_name, $parameters, &$return)
{
  $return_value = "You called ${method_name}!";
}

as a member function will do the job.

Adam Wright
I suspect jes5199 was thinking of the __call method when he asked if anyone knew anything like the __call method... I think "in PHP4" was the critical part of the question.
reefnet_alex
Yes, and I was referring to PHP4. Note the change in signature, which changed in 5.
Adam Wright
ah, it looks like you're right, but also (a vital bit that I was missing), you must use the overload("ClassName") function to *enable* the __call method.
jes5199
+1  A: 

This article, Using Method Call Overloading in PHP 4 over on DevShed might help.

Novaktually
+2  A: 

The most important bit that I was missing was that __call exists in PHP4, but you must enable it on a per-class basis by calling overload(), as seen in php docs here . Unfortunately, the __call() function signatures are different between PHP4 and PHP5, and there does not seem to be a way to make an implementation that will run in both.

jes5199