Is there a way to emulate PHP5's __call() magic method in PHP4?

大憨熊 提交于 2019-12-11 03:54:18

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/76328/is-there-a-way-to-emulate-php5s-call-magic-method-in-php4

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!