mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-16 00:48:54 +00:00
Added __set_state() example (same in var_export()) and __callStatic() in list.
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@251213 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
75db80b3e0
commit
f193f6c844
1 changed files with 49 additions and 3 deletions
|
@ -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 @@ Hello
|
|||
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>
|
||||
|
||||
|
|
Loading…
Reference in a new issue