用__call()实现方法重载
2011-06-22 15:05
357 查看
今天在网上发现了一片好文章,介绍_call()方法。 依靠这个方法可以实现方法重载,这是找了好久的东西了。 发在这里,算作笔记。 --------------------------------------- PHP5 的对象新增了一个专用方法 __call(),这个方法用来监视一个对象中的其它方法。如果你试着调用一个对象中不存在的方法,__call 方法将会被自动调用。 例七:__call <?php class foo { function __call($name,$arguments) { print("Did you call me? I'm $name!"); } } $x = new foo(); $x->doStuff(); $x->fancy_stuff(); ?> 这个特殊的方法可以被用来实现“过载(overloading)”的动作,这样你就可以检查你的参数并且通过调用一个私有的方法来传递参数。 例八:使用 __call 实现“过载”动作 <?php class Magic { function __call($name,$arguments) { if($name=='foo') { if(is_int($arguments[0])) $this->foo_for_int($arguments[0]); if(is_string($arguments[0])) $this->foo_for_string($arguments[0]); } } private function foo_for_int($x) { print("oh an int!"); } private function foo_for_string($x) { print("oh a string!"); } } $x = new Magic(); $x->foo(3); $x->foo("3"); ?> |
相关文章推荐
- PHP5学习笔记:用__call()实现方法重载
- php用魔术方法__call实现类函数重载
- __call 实现函数方法不同参数个数的重载
- PHP5学习笔记:用__call()实现方法重载
- Javascript中的重载的实现以及方法apply和call
- PHP5学习笔记:用__call()实现方法重载
- php __call方法 即使用其实现方法重载
- C#方法参数个数和类型相同,名称不同,可以实现重载吗
- 浅谈java实现重载的方法
- react 调用 native 的callShareAllFunc()方法,实现分享
- C++通过构造器,实现对象初始化的两种方法(重载、默认参数)
- 扩展方法Select无重载单一方式实现示例
- php实现方法重载
- PHP魔术方法之__call重载方法
- JS面向对象(3) -- Object类,静态属性,闭包,私有属性, call和apply的使用,继承的三种实现方法
- JS重载实现方法分析
- PHP中实现重载的方法
- js面向对象编程之如何实现方法重载
- js中call()方法实现类的继承
- PHP使用方法重载实现动态创建属性的get和set方法