cvs: phpdoc /en/language/oop5 magic.xml
| From: | Felipe Pena | Date: | Wed, 23 Jan 2008 22:34:20 +0000 |
| Subject: | cvs: phpdoc /en/language/oop5 magic.xml | ||
| Groups: | php.doc.cvs | ||
| Request: | Send a blank email to doc-cvs+get-1065@lists.php.net to get a copy of this message | ||
felipe Wed Jan 23 22:34:20 2008 UTC
Modified files:
/phpdoc/en/language/oop5 magic.xml
Log:
Added __set_state() example (same in var_export()) and __callStatic() in list.
http://cvs.php.net/viewvc.cgi/phpdoc/en/language/oop5/magic.xml?r1=1.19&r2=1.20&diff_format=u
Index: phpdoc/en/language/oop5/magic.xml
diff -u phpdoc/en/language/oop5/magic.xml:1.19 phpdoc/en/language/oop5/magic.xml:1.20
--- phpdoc/en/language/oop5/magic.xml:1.19 Tue Nov 20 02:05:41 2007
+++ phpdoc/en/language/oop5/magic.xml Wed Jan 23 22:34:20 2008
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
-<!-- $Revision: 1.19 $ -->
+<!-- $Revision: 1.20 $ -->
<sect1 xml:id="language.oop5.magic" xmlns="http://docbook.org/ns/docbook">
<title>Magic Methods</title>
<para>
@@ -8,16 +8,17 @@
<literal>__destruct</literal>
(see <link linkend="language.oop5.decon">Constructors and
Destructors</link>),
<literal>__call</literal>,
+ <literal>__callStatic</literal>,
<literal>__get</literal>,
<literal>__set</literal>,
<literal>__isset</literal>,
- <literal>__unset</literal>
+ <literal>__unset</literal>
(see <link linkend="language.oop5.overloading">Overloading</link>),
<literal>__sleep</literal>,
<literal>__wakeup</literal>,
<literal>__toString</literal>,
<literal>__set_state</literal> and
- <link linkend="language.oop5.cloning">__clone</link>
+ <link linkend="language.oop5.cloning">__clone</link>
are magical in PHP classes. You
cannot have functions with these names in any of your
classes unless you want the magic functionality associated
@@ -160,6 +161,51 @@
The only parameter of this method is an array containing exported
properties in the form <literal>array('property' => value,
...)</literal>.
</para>
+ <example>
+ <title>Using <literal>__set_state</literal> (since PHP 5.1.0)</title>
+ <programlisting role="php">
+<![CDATA[
+<?php
+
+class A
+{
+ public $var1;
+ public $var2;
+
+ public static function __set_state($an_array) // As of PHP 5.1.0
+ {
+ $obj = new A;
+ $obj->var1 = $an_array['var1'];
+ $obj->var2 = $an_array['var2'];
+ return $obj;
+ }
+}
+
+$a = new A;
+$a->var1 = 5;
+$a->var2 = 'foo';
+
+eval('$b = ' . var_export($a, true) . ';'); // $b = A::__set_state(array(
+ // 'var1' => 5,
+ // 'var2' => 'foo',
+ // ));
+var_dump($b);
+
+?>
+]]>
+ </programlisting>
+ &example.outputs;
+ <screen>
+<![CDATA[
+object(A)#2 (2) {
+ ["var1"]=>
+ int(5)
+ ["var2"]=>
+ string(3) "foo"
+}
+]]>
+ </screen>
+ </example>
</sect2>
</sect1>