PHP5带来了强大的面向对象重载,允许程序员建立自定义的行为来访问属性和调用方法,php5加入了如下的内部特征 __construct(); 初始化--构造函数 __destruct(); 卸载--析构函数 __get(); __get方法可以用来捕获一个对象中不存在的变量和方法 __set(); __set方法可以用来捕获和按参数修改一个对象中不存在的变量和方法 __call(); 调用不存在的类的函数的时候得处理方法 __clone(); copy对象用clone $obj; __sleep(); 串行化的时候用 __wakeup(); 反串行化的时候用 重载可以通过__get, __set, and __call几个特殊方法来进行. 当Zend引擎试图访问一个成员并没有找到时,PHP将会调用这些方法. 在例6.14中,__get和__set代替所有对属性变量数组的访问. 如果必要,你可以实现任何类型你想要的过滤. 例如,脚本可以禁止设置属性值, 在开始时用一定的前缀或包含一定类型的值. __call方法说明了你如何调用未经定义的方法. 你调用未定义方法时,方法名和方法接收的参数将会传给__call方法, PHP传递__call的值返回给未定义的方法.
<?php class foo { function __set($name,$val) { print(" Hello, you tried to put $val in $name"); } function __get($name) { print(" Hey you asked for $name"); } } $x = new foo(); $x->bar = 3;//注意$bar不存在 print($x->winky_winky); ?> <?php class foo { function __call($name,$arguments) { print("Did you call me? I m $name!"); } } $x = new foo(); $x->doStuff(2); $x->fancy_stuff("a string"); ?>这个特殊的方法可以被用来实现[过载(overloading)]的动作,这样你就可以检查你的参数并且通过调用一个私有的方法来传递参数。 使用 __call 实现[过载]动作
<?php class Magic { function __call($name,$arguments) { if($name=="foo") { print_r($arguments); 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"); ?>对一个对象的拷贝通过调用对象的__clone()方法完成
<?php $copy_of_object =clone $obj; ?> <?php class MyCloneable { static $id = 0; function MyCloneable() { $this->id = self::$id+1; //注意这里如果写self::$id++;将不被充许 } function __clone() { $this->address = "New York"; $this->id = self::$id+1; } } $obj = new MyCloneable(); $obj->name = "Hello"; $obj->address = "Tel-Aviv"; print $obj->id . " "; $obj =clone $obj; print $obj->id . " "; print $obj->name . " "; print $obj->address . " "; ?>
查看更多关于PHP5中__call__get__set__clone__sleep__wakeup用法_自学ph的详细内容...