2

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?

Jeff Atwood
  • 63,320
  • 48
  • 150
  • 153
jes5199
  • 18,324
  • 12
  • 36
  • 40
  • This article, [Using Method Call Overloading in PHP 4 ](http://www.devshed.com/c/a/PHP/Using-Method-Call-Overloading-in-PHP-4/) over on DevShed might help. –  Sep 16 '08 at 20:11

2 Answers2

2

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
  • 18,324
  • 12
  • 36
  • 40
0

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
  • 48,938
  • 12
  • 131
  • 152
  • 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 Sep 16 '08 at 20:13
  • Yes, and I was referring to PHP4. Note the change in signature, which changed in 5. – Adam Wright Sep 16 '08 at 23:31
  • 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 Sep 21 '08 at 23:59