/** PHP把所有以__(两个下划线)开头的类方法当成魔术方法。所以你定义自己的类方法时,不要以 __为前缀。 * */</P><P>// __toString、__set、__get__isset()、__unset()<BR>/*<BR> The __toString method allows a class to decide how it will react when it is converted to a string.<BR> __set() is run when writing data to inaccessible members.<BR> __get() is utilized for reading data from inaccessible members.<BR> __isset() is triggered by calling isset() or empty() on inaccessible members.<BR> __unset() is invoked when unset() is used on inaccessible members.<BR> */<BR>class TestClass {</P><P> privat<b style="color:transparent">(、本文来源gao@!dai!ma.com搞$$代^@码网*</b><i>搞gaodaima代码</i>e $data = array();<BR> public $foo;</P><P> public function __construct($foo) {<BR> $this->foo = $foo;<BR> }</P><P> public function __toString() {<BR> return $this->foo;<BR> }</P><P> public function __set($name, $value) {<BR> echo "__set, Setting '$name' to '$value'\n";<BR> $this->data[$name] = $value;<BR> }</P><P> public function __get($name) {<BR> echo "__get, Getting '$name'\n";<BR> if (array_key_exists($name, $this->data)) {<BR> return $this->data[$name];<BR> }<BR> }</P><P> /** As of PHP 5.1.0 */<BR> public function __isset($name) {<BR> echo "__isset, Is '$name' set?\n";<BR> return isset($this->data[$name]);<BR> }</P><P> /** As of PHP 5.1.0 */<BR> public function __unset($name) {<BR> echo "__unset, Unsetting '$name'\n";<BR> unset($this->data[$name]);<BR> }</P><P>}</P><P>$obj = new TestClass('Hello');<BR>echo "__toString, $obj\n";<BR>$obj->a = 1;<BR>echo $obj->a . "\n\n";<BR>var_dump(isset($obj->a));<BR>unset($obj->a);<BR>var_dump(isset($obj->a));<BR>echo "\n\n"; <BR>/**<BR> 输出结果如下:<BR> __toString, Hello<BR> __set, Setting 'a' to '1'<BR> __get, Getting 'a'<BR> __isset, Is 'a' set?<BR> bool(true)<BR> __unset, Unsetting 'a'<BR> __isset, Is 'a' set?<BR> bool(false)<BR> **/</P><P> </P><P>// __call __callStatic<BR>/*<BR> mixed __call ( string $name , array $arguments )<BR> mixed __callStatic ( string $name , array $arguments )<BR> __call() is triggered when invoking inaccessible methods in an object context.<BR> __callStatic() is triggered when invoking inaccessible methods in a static context.<BR> The $name argument is the name of the method being called.<BR> The $arguments argument is an enumerated array containing the parameters passed to the $name'ed method.<BR> */<BR>class MethodTest {<BR> public function __call($name, $arguments) {<BR> // Note: value of $name is case sensitive.<BR> echo "__call, Calling object method '$name' " . implode(', ', $arguments) . "\n";<BR> }</P><P> /** As of PHP 5.3.0 */<BR> public static function __callStatic($name, $arguments) {<BR> // Note: value of $name is case sensitive.<BR> echo "__callStatic, Calling static method '$name' " . implode(', ', $arguments) . "\n";<BR> }</P><P>}</P><P>$obj = new MethodTest;<BR>$obj->runTest('in object context', 'param2', 'param3');<BR>//MethodTest::runTest('in static context'); // As of PHP 5.3.0<BR>echo "\n\n"; <BR>/**<BR> 输出结果如下:<BR> __call, Calling object method 'runTest' in object context, param2, param3<BR> string(10) "__invoke: "<BR> */</P><P> </P><P>// __invoke<BR>/* <BR> The __invoke method is called when a script tries to call an object as a function.<BR> Note: This feature is available since PHP 5.3.0.<BR>*/<BR>class CallableClass {<BR> function __invoke($x) {<BR> var_dump($x);<BR> }<BR>}</P><P>$obj = new CallableClass;<BR>//$obj(5);<BR>var_dump('__invoke: ' . is_callable($obj));<BR>echo "\n\n"; </P><P> </P><P> </P><P>// __sleep __wakeup<BR>/*<BR> 串行化serialize可以把变量包括对象,转化成连续bytes数据. 你可以将串行化后的变量存在一个文件里或在网络上传输. <BR> 然后再反串行化还原为原来的数据. 你在反串行化类的对象之前定义的类,PHP可以成功地存储其对象的属性和方法. <BR> 有时你可能需要一个对象在反串行化后立即执行. 为了这样的目的,PHP会自动寻找__sleep和__wakeup方法.<BR> 当一个对象被串行化,PHP会调用__sleep方法(如果存在的话). 在反串行化一个对象后,PHP 会调用__wakeup方法. <BR> 这两个方法都不接受参数. __sleep方法必须返回一个数组,包含需要串行化的属性. PHP会抛弃其它属性的值. <BR> 如果没有__sleep方法,PHP将保存所有属性.下面的例子显示了如何用__sleep和__wakeup方法来串行化一个对象. <BR> Id属性是一个不打算保留在对象中的临时属性. __sleep方法保证在串行化的对象中不包含id属性. <BR> 当反串行化一个User对象,__wakeup方法建立id属性的新值. 这个例子被设计成自我保持. <BR> 在实际开发中,你可能发现包含资源(如图像或数据流)的对象需要这些方法<BR> */</P><P>class User {</P><P> public $name;<BR> public $id;</P><P> function __construct() {<BR> //give user a unique ID 赋予一个差别 的ID<BR> $this->id = uniqid();<BR> }</P><P> //__sleep返回值的类型是数组,数组中的值是不需要串型化的字段id</P><P> function __sleep() {<BR> //do not serialize this->id 不串行化id<BR> return(array("name"));<BR> }</P><P> function __wakeup() {<BR> //give user a unique ID<BR> $this->id = uniqid();<BR> }</P><P>}</P><P>//create object 成立一个器材<BR>$u = new User;<BR>$u->name = "Leon"; //serialize it 串行化 留意不串行化id属性,id的值被遗弃<BR>$s = serialize($u);<BR>echo "__sleep, __wakeup, s: $s"; //unserialize it 反串行化 id被重新赋值<BR>$u2 = unserialize($s); //$u and $u2 have different IDs $u和$u2有差别 的ID<BR>print_r($u);<BR>print_r($u2);<BR>echo "\n\n"; <BR>/**<BR> 输出结果如下:<BR> __sleep, __wakeup, s: O:4:"User":1:{s:4:"name";s:4:"Leon";}<BR> User Object<BR> (<BR> [name] => Leon<BR> [id] => 4db1b17640da1<BR> )<BR> User Object<BR> (<BR> [name] => Leon<BR> [id] => 4db1b17640dbc<BR> )<BR> */</P><P><BR>// __set_state<BR>/*<BR> This static method is called for classes exported by var_export() since PHP 5.1.0.<BR> The only parameter of this method is an array containing exported properties in the form array('property' => value, ...).<BR> */</P><P>class A {</P><P> public $var1;<BR> public $var2;</P><P> public static function __set_state($an_array) { // As of PHP 5.1.0<BR> //$an_array打印出来是数组,而不是调用时传递的对象<BR> print_r($an_array);<BR> $obj = new A;<BR> $obj->var1 = $an_array['var1'];<BR> $obj->var2 = $an_array['var2'];<BR> return $obj;<BR> }</P><P>}</P><P>$a = new A;<BR>$a->var1 = 5;<BR>$a->var2 = 'foo';<BR>echo "__set_state:\n";<BR>eval('$b = ' . var_export($a, true) . ';'); <BR>// $b = A::__set_state(array(<BR>// 'var1' => 5,<BR>// 'var2' => 'foo',<BR>// ));<BR>var_dump($b);<BR>echo "\n\n"; <BR>/**<BR> 输出结果如下:<BR> __set_state:<BR> Array<BR> (<BR> [var1] => 5<BR> [var2] => foo<BR> )<BR> object(A)#5 (2) {<BR> ["var1"]=><BR> int(5)<BR> ["var2"]=><BR> string(3) "foo"<BR> }<BR> */</P><P> </P><P>// __clone<BR>class SubObject {</P><P> static $instances = 0;<BR> public $instance;</P><P> public function __construct() {<BR> $this->instance = ++self::$instances;<BR> }</P><P> public function __clone() {<BR> $this->instance = ++self::$instances;<BR> }</P><P>}</P><P>class MyCloneable {</P><P> public $object1;<BR> public $object2;</P><P> function __clone() {<BR> // Force a copy of this->object, otherwise<BR> // it will point to same object.<BR> $this->object1 = clone $this->object1;<BR> }</P><P>}</P><P>$obj = new MyCloneable();<BR>$obj->object1 = new SubObject();<BR>$obj->object2 = new SubObject();<BR>$obj2 = clone $obj;<BR>print("__clone, Original Object:\n");<BR>print_r($obj);<BR>print("__clone, Cloned Object:\n");<BR>print_r($obj2);<BR>echo "\n\n";<BR>/**<BR> 输出结果如下:<BR> __clone, Original Object:<BR> MyCloneable Object<BR> (<BR> [object1] => SubObject Object<BR> (<BR> [instance] => 1<BR> ) [object2] => SubObject Object<BR> (<BR> [instance] => 2<BR> ))<BR> __clone, Cloned Object:<BR> MyCloneable Object<BR> (<BR> [object1] => SubObject Object<BR> (<BR> [instance] => 3<BR> ) [object2] => SubObject Object<BR> (<BR> [instance] => 2<BR> )) <BR> */<BR>