cvs: phpdoc-zh /language/oop5 abstract.xml cloning.xml constants.xml final.xml interfaces.xml iterations.xml magic.xml object-comparison.xml
overloading.xml

From: Date: Sat, 13 Dec 2008 03:45:30 +0000
Subject: cvs: phpdoc-zh /language/oop5 abstract.xml cloning.xml constants.xml final.xml interfaces.xml iterations.xml magic.xml object-comparison.xml
overloading.xml
Groups: php.doc.zh 
Request: Send a blank email to doc-zh+get-3048@lists.php.net to get a copy of this message
haohappy Sat Dec 13 03:45:30 2008 UTC Modified files: /phpdoc-zh/language/oop5 abstract.xml cloning.xml constants.xml final.xml interfaces.xml iterations.xml magic.xml object-comparison.xml overloading.xml Log: new files added

http://cvs.php.net/viewvc.cgi/phpdoc-zh/language/oop5/abstract.xml?r1=1.2&r2=1.3&diff_format=u Index: phpdoc-zh/language/oop5/abstract.xml diff -u /dev/null phpdoc-zh/language/oop5/abstract.xml:1.3 --- /dev/null Sat Dec 13 03:45:30 2008 +++ phpdoc-zh/language/oop5/abstract.xml Sat Dec 13 03:45:30 2008 @@ -0,0 +1,103 @@ +<?xml version="1.0" encoding="gb2312"?> +<!-- $Revision: 1.3 $ --> +<!-- $Author: haohappy $ --> +<!-- EN-Revision: 1.15 Maintainer: haohappy Status: ready --> + <sect1 xml:id="language.oop5.abstract" xmlns="http://docbook.org/ns/docbook"> + <title>抽象类</title> + + <para> + PHP5支持抽象类和抽象方法。抽象类不能直接被实例化,你必须先继承该抽象类,然后再实例化子类。抽象类中 + 至少要包含一个抽象方法。如果类方法被声明为抽象的,那么其中就不能包括具体的功能实现。 + </para> + + <para> + 继承一个抽象类的时候,子类必须实现抽象类中的所有抽象方法;另外,这些方法的<link linkend="language.oop5.visibility">可见性</link> + 必须和抽象类中一样(或者更为宽松)。如果抽象类中某个抽象方法被声明为protected,那么子类中实现的方法就应该声明为protected或者public,而不 + 能定义为private。 + </para> + + <example> + <title>抽象类代码示例</title> + <programlisting role="php"> +<![CDATA[ +<?php +abstract class AbstractClass +{ + // 强制要求子类定义这些方法 + abstract protected function getValue(); + abstract protected function prefixValue($prefix); + + // 普通方法(非抽象方法) + public function printOut() { + print $this->getValue() . "\n"; + } +} + +class ConcreteClass1 extends AbstractClass +{ + protected function getValue() { + return "ConcreteClass1"; + } + + public function prefixValue($prefix) { + return "{$prefix}ConcreteClass1"; + } +} + +class ConcreteClass2 extends AbstractClass +{ + public function getValue() { + return "ConcreteClass2"; + } + + public function prefixValue($prefix) { + return "{$prefix}ConcreteClass2"; + } +} + +$class1 = new ConcreteClass1; +$class1->printOut(); +echo $class1->prefixValue('FOO_') ."\n"; + +$class2 = new ConcreteClass2; +$class2->printOut(); +echo $class2->prefixValue('FOO_') ."\n"; +?> +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +ConcreteClass1 +FOO_ConcreteClass1 +ConcreteClass2 +FOO_ConcreteClass2 +]]> + </screen> + </example> + + <para> + 抽象类的子类中的普通方法执行方式和其他类相同。 + </para> + </sect1> + +<!-- Keep this comment at the end of the file +Local variables: +mode: sgml +sgml-omittag:t +sgml-shorttag:t +sgml-minimize-attributes:nil +sgml-always-quote-attributes:t +sgml-indent-step:1 +sgml-indent-data:t +indent-tabs-mode:nil +sgml-parent-document:nil +sgml-default-dtd-file:"../../manual.ced" +sgml-exposed-tags:nil +sgml-local-catalogs:nil +sgml-local-ecat-files:nil +End: +vim600: syn=xml fen fdm=syntax fdl=2 si +vim: et tw=78 syn=sgml +vi: ts=1 sw=1 +--> http://cvs.php.net/viewvc.cgi/phpdoc-zh/language/oop5/cloning.xml?r1=1.2&r2=1.3&diff_format=u Index: phpdoc-zh/language/oop5/cloning.xml diff -u /dev/null phpdoc-zh/language/oop5/cloning.xml:1.3 --- /dev/null Sat Dec 13 03:45:30 2008 +++ phpdoc-zh/language/oop5/cloning.xml Sat Dec 13 03:45:30 2008 @@ -0,0 +1,143 @@ +<?xml version="1.0" encoding="gb2312"?> +<!-- $Revision: 1.3 $ --> +<!-- $Author: haohappy $ --> +<!-- EN-Revision: 1.6 Maintainer: haohappy Status: ready --> + <sect1 xml:id="language.oop5.cloning" xmlns="http://docbook.org/ns/docbook"> + <title>对象复制</title> + + <para> + 在多数情况下,我们并不需要完全复制一个对象来获得其中属性。但有一个情况下确实需要:如果你有一个 + GTK窗口对象,该对象持有窗口相关的资源。你可能会想复制一个新的窗口,保持所有属性与原来的窗口相同, + 但必须是一个新的对象(因为如果不是新的对象,那么一个窗口中的改变就会影响到另一个窗口)。还有一种情况: + 如果对象A中保存着对象B的引用,当你复制对象A时,你想其中使用的对象不再是对象B而是B的一个副本,那么 + 你必须得到对象A的一个副本。 + + </para> + + <para> + 对象复制可以通过clone关键字来完成(如果对象中存在__clone()方法,会先被调用)。对象中的 + __clone()方法不能直接调用。 + </para> + + <informalexample> + <programlisting> +<![CDATA[ +$copy_of_object = clone $object; +]]> + </programlisting> + </informalexample> + + <para> + 当对象被复制后,PHP5会对对象的所有属性执行一个“浅复制”(shallow copy)。所有的属性中的引用 + 仍然不变,指向原来的变量。如果定义了__clone()方法,则新创建的对象(复制生成的对象)中的__clone()方法会被调用, + 可用于修改属性的值(如果有必要的话)。 + </para> + + <example> + <title>复制一个对象</title> + <programlisting role="php"> +<![CDATA[ +<?php +class SubObject +{ + static $instances = 0; + public $instance; + + public function __construct() { + $this->instance = ++self::$instances; + } + + public function __clone() { + $this->instance = ++self::$instances; + } +} + +class MyCloneable +{ + public $object1; + public $object2; + + function __clone() + { + + // 强制复制一份this->object, 否则仍然指向同一个对象 + $this->object1 = clone $this->object1; + } +} + +$obj = new MyCloneable(); + +$obj->object1 = new SubObject(); +$obj->object2 = new SubObject(); + +$obj2 = clone $obj; + + +print("Original Object:\n"); +print_r($obj); + +print("Cloned Object:\n"); +print_r($obj2); + +?> +]]> + </programlisting> + &example.outputs; + <screen role="php"> +<![CDATA[ +Original Object: +MyCloneable Object +( + [object1] => SubObject Object + ( + [instance] => 1 + ) + + [object2] => SubObject Object + ( + [instance] => 2 + ) + +) +Cloned Object: +MyCloneable Object +( + [object1] => SubObject Object + ( + [instance] => 3 + ) + + [object2] => SubObject Object + ( + [instance] => 2 + ) + +) +]]> + + </screen> + + </example> + + </sect1> + +<!-- Keep this comment at the end of the file +Local variables: +mode: sgml +sgml-omittag:t +sgml-shorttag:t +sgml-minimize-attributes:nil +sgml-always-quote-attributes:t +sgml-indent-step:1 +sgml-indent-data:t +indent-tabs-mode:nil +sgml-parent-document:nil +sgml-default-dtd-file:"../../manual.ced" +sgml-exposed-tags:nil +sgml-local-catalogs:nil +sgml-local-ecat-files:nil +End: +vim600: syn=xml fen fdm=syntax fdl=2 si +vim: et tw=78 syn=sgml +vi: ts=1 sw=1 +--> http://cvs.php.net/viewvc.cgi/phpdoc-zh/language/oop5/constants.xml?r1=1.2&r2=1.3&diff_format=u Index: phpdoc-zh/language/oop5/constants.xml diff -u /dev/null phpdoc-zh/language/oop5/constants.xml:1.3 --- /dev/null Sat Dec 13 03:45:30 2008 +++ phpdoc-zh/language/oop5/constants.xml Sat Dec 13 03:45:30 2008 @@ -0,0 +1,96 @@ +<?xml version="1.0" encoding="gb2312"?> +<!-- $Revision: 1.3 $ --> +<!-- $Author: haohappy $ --> +<!-- EN-Revision: 1.17 Maintainer: haohappy Status: ready --> + <sect1 xml:id="language.oop5.constants" xmlns="http://docbook.org/ns/docbook"> + <title>类常量</title> + <para> + 我们可以在类中定义常量。常量的值将始终保持不变。在定义和使用常量的时候不需要使用<varname>$</varname>符号。 + </para> + <para> + 常量的值必须是一个定值,不能是变量,类属性或其它操作(如函数调用)的结果。 + </para> + <para> + Its also possible for interfaces to have <literal>constants</literal>. Look at + the <link linkend="language.oop5.interfaces">interface documentation</link> for + examples. + + 接口(interface)中也可以定义常量。请查看<link linkend="language.oop5.interfaces">接口的文档</link>获得更多示例。 + </para> + <para> + PHP5.3.0之后,我们可以用一个变量来动态调用类。但该变量的值不能为关键字<literal>self</literal>, + <literal>parent</literal> 或<literal>static</literal>。 + </para> + <example> + <title>定义和使用一个类常量</title> + <programlisting role="php"> +<![CDATA[ +<?php +class MyClass +{ + const constant = 'constant value'; + + function showConstant() { + echo self::constant . "\n"; + } +} + +echo MyClass::constant . "\n"; + +$classname = "MyClass"; +echo $classname::constant . "\n"; // PHP 5.3.0之后 + +$class = new MyClass(); +$class->showConstant(); + +echo $class::constant."\n"; // PHP 5.3.0之后 +?> +]]> + </programlisting> + </example> + + <example> + <title>静态数据示例</title> + <programlisting role="php"> +<![CDATA[ +<?php +class foo { + // PHP 5.3.0之后 + const bar = <<<'EOT' +bar +EOT; +} +?> +]]> + </programlisting> + <para> + 和heredocs(字符串边界符)不同,nowdocs可以用在任何静态数据中。 + </para> + </example> + <note> + <para> + PHP 5.3.0起PHP支持Nowdoc功能。 + </para> + </note> + </sect1> + +<!-- Keep this comment at the end of the file +Local variables: +mode: sgml +sgml-omittag:t +sgml-shorttag:t +sgml-minimize-attributes:nil +sgml-always-quote-attributes:t +sgml-indent-step:1 +sgml-indent-data:t +indent-tabs-mode:nil +sgml-parent-document:nil +sgml-default-dtd-file:"../../manual.ced" +sgml-exposed-tags:nil +sgml-local-catalogs:nil +sgml-local-ecat-files:nil +End: +vim600: syn=xml fen fdm=syntax fdl=2 si +vim: et tw=78 syn=sgml +vi: ts=1 sw=1 +--> http://cvs.php.net/viewvc.cgi/phpdoc-zh/language/oop5/final.xml?r1=1.2&r2=1.3&diff_format=u Index: phpdoc-zh/language/oop5/final.xml diff -u /dev/null phpdoc-zh/language/oop5/final.xml:1.3 --- /dev/null Sat Dec 13 03:45:30 2008 +++ phpdoc-zh/language/oop5/final.xml Sat Dec 13 03:45:30 2008 @@ -0,0 +1,82 @@ +<?xml version="1.0" encoding="gb2312"?> +<!-- $Revision: 1.3 $ --> +<!-- $Author: haohappy $ --> +<!-- EN-Revision: 1.6 Maintainer: haohappy Status: ready --> + <sect1 xml:id="language.oop5.final" xmlns="http://docbook.org/ns/docbook"> + <title>Final关键字</title> + <para> + PHP 5 新增了一个 final 关键字。如果父类中的方法被声明为final,则子类无法覆盖该方法; + 如果一个类被声明为final,则不能被继承。 + </para> + + <example> + <title>Final 方法示例</title> + <programlisting role="php"> +<![CDATA[ +<?php +class BaseClass { + public function test() { + echo "BaseClass::test() called\n"; + } + + final public function moreTesting() { + echo "BaseClass::moreTesting() called\n"; + } +} + +class ChildClass extends BaseClass { + public function moreTesting() { + echo "ChildClass::moreTesting() called\n"; + } +} +// 产生 Fatal error: Cannot override final method BaseClass::moreTesting() +?> +]]> + </programlisting> + </example> + + <example> + <title>Final 类示例</title> + <programlisting role="php"> +<![CDATA[ +<?php +final class BaseClass { + public function test() { + echo "BaseClass::test() called\n"; + } + + // 这里无论你是否将方法声明为final,都没有关系 + final public function moreTesting() { + echo "BaseClass::moreTesting() called\n"; + } +} + +class ChildClass extends BaseClass { +} +// 产生 Fatal error: Class ChildClass may not inherit from final class (BaseClass) +?> +]]> + </programlisting> + </example> + </sect1> + +<!-- Keep this comment at the end of the file +Local variables: +mode: sgml +sgml-omittag:t +sgml-shorttag:t +sgml-minimize-attributes:nil +sgml-always-quote-attributes:t +sgml-indent-step:1 +sgml-indent-data:t +indent-tabs-mode:nil +sgml-parent-document:nil +sgml-default-dtd-file:"../../manual.ced" +sgml-exposed-tags:nil +sgml-local-catalogs:nil +sgml-local-ecat-files:nil +End: +vim600: syn=xml fen fdm=syntax fdl=2 si +vim: et tw=78 syn=sgml +vi: ts=1 sw=1 +--> http://cvs.php.net/viewvc.cgi/phpdoc-zh/language/oop5/interfaces.xml?r1=1.2&r2=1.3&diff_format=u Index: phpdoc-zh/language/oop5/interfaces.xml diff -u /dev/null phpdoc-zh/language/oop5/interfaces.xml:1.3 --- /dev/null Sat Dec 13 03:45:30 2008 +++ phpdoc-zh/language/oop5/interfaces.xml Sat Dec 13 03:45:30 2008 @@ -0,0 +1,221 @@ +<?xml version="1.0" encoding="gb2312"?> +<!-- $Revision: 1.3 $ --> +<!-- $Author: haohappy $ --> +<!-- EN-Revision: 1.14 Maintainer: haohappy Status: ready --> + <sect1 xml:id="language.oop5.interfaces" xmlns="http://docbook.org/ns/docbook"> + <title>接口</title> + <para> + 使用接口(interface),你可以指定某个类必须实现哪些方法,但不需要定义这些方法的具体内容。 + </para> + <para> + 我们可以通过interface来定义一个接口,就像定义一个标准的类一样,但其中定义所有的方法都是空的。 + </para> + <para> + 接口中定义的所有方法都必须是public,这是接口的特性。 + </para> + <sect2 xml:id="language.oop5.interfaces.implements"> + <title><literal>实现</literal></title> + <para> + 要实现一个接口,可以使用<literal>implements</literal>操作符。类中必须实现接口中定义的所有方法,否则 + 会报一个fatal错误。如果要实现多个接口,可以用逗号来分隔多个接口的名称。 + </para> + <note> + <para> + 实现多个接口时,接口中的方法不能有重名。 + </para> + </note> + <note> + <para> + 接口也可以继承,通过使用<literal>extends</literal>操作符。 + </para> + </note> + </sect2> + <sect2 xml:id="language.oop5.interfaces.constants"> + <title><literal>常量</literal></title> + <para> + 接口中也可以定义常量。接口常量和<link linkend="language.oop5.constants">类常量</link>的使用完全相同。 + 它们都是定值,不能被子类或子接口修改。 + </para> + </sect2> + <sect2 xml:id="language.oop5.interfaces.examples"> + &reftitle.examples; + <example xml:id="language.oop5.interfaces.examples.ex1"> + <title>接口代码示例</title> + <programlisting role="php"> +<![CDATA[ +<?php + +// 声明一个'iTemplate'接口 +interface iTemplate +{ + public function setVariable($name, $var); + public function getHtml($template); +} + + +// 实现接口 +// 下面的写法是正确的 +class Template implements iTemplate +{ + private $vars = array(); + + public function setVariable($name, $var) + { + $this->vars[$name] = $var; + } + + public function getHtml($template) + { + foreach($this->vars as $name => $value) { + $template = str_replace('{' . $name . '}', $value, $template); + } + + return $template; + } +} + +// 下面的写法是错误的,会报错: +// Fatal error: Class BadTemplate contains 1 abstract methods +// and must therefore be declared abstract (iTemplate::getHtml) +class BadTemplate implements iTemplate +{ + private $vars = array(); + + public function setVariable($name, $var) + { + $this->vars[$name] = $var; + } +} +?> +]]> + </programlisting> + </example> + <example xml:id="language.oop5.interfaces.examples.ex2"> + <title>Extendable Interfaces</title> + <programlisting role="php"> +<![CDATA[ +<?php +interface a +{ + public function foo(); +} + +interface b extends a +{ + public function baz(Baz $baz); +} + +// 正确写法 +class c implements b +{ + public function foo() + { + } + + public function baz(Baz $baz) + { + } +} + +// 错误写法会导致一个fatal error +class d implements b +{ + public function foo() + { + } + + public function baz(Foo $foo) + { + } +} +?> +]]> + </programlisting> + </example> + <example xml:id="language.oop5.interfaces.examples.ex3"> + <title>多个接口间的继承</title> + <programlisting role="php"> +<![CDATA[ +<?php +interface a +{ + public function foo(); +} + +interface b +{ + public function bar(); +} + +interface c extends a, b +{ + public function baz(); +} + +class d implements c +{ + public function foo() + { + } + + public function bar() + { + } + + public function baz() + { + } +} +?> +]]> + </programlisting> + </example> + <example xml:id="language.oop5.interfaces.examples.ex4"> + <title>使用接口常量</title> + <programlisting role="php"> +<![CDATA[ +<?php +interface a +{ + const b = 'Interface constant'; +} + +// 输出接口常量 +echo a::b; + +// 错误写法,因为常量的值不能被修改。接口常量的概念和类常量是一样的。 +class b implements a +{ + const b = 'Class constant'; +} +?> +]]> + </programlisting> + </example> + <simpara> + 你也可以看看<link linkend="language.operators.type">instanceof</link>关键字的文档。 + </simpara> + </sect2> + + </sect1> + +<!-- Keep this comment at the end of the file +Local variables: +mode: sgml +sgml-omittag:t +sgml-shorttag:t +sgml-minimize-attributes:nil +sgml-always-quote-attributes:t +sgml-indent-step:1 +sgml-indent-data:t +indent-tabs-mode:nil +sgml-parent-document:nil +sgml-default-dtd-file:"../../manual.ced" +sgml-exposed-tags:nil +sgml-local-catalogs:nil +sgml-local-ecat-files:nil +End: +vim600: syn=xml fen fdm=syntax fdl=2 si +vim: et tw=78 syn=sgml +vi: ts=1 sw=1 +--> http://cvs.php.net/viewvc.cgi/phpdoc-zh/language/oop5/iterations.xml?r1=1.2&r2=1.3&diff_format=u Index: phpdoc-zh/language/oop5/iterations.xml diff -u /dev/null phpdoc-zh/language/oop5/iterations.xml:1.3 --- /dev/null Sat Dec 13 03:45:30 2008 +++ phpdoc-zh/language/oop5/iterations.xml Sat Dec 13 03:45:30 2008 @@ -0,0 +1,258 @@ +<?xml version="1.0" encoding="gb2312"?> +<!-- $Revision: 1.3 $ --> +<!-- $Author: haohappy $ --> +<!-- EN-Revision: 1.13 Maintainer: haohappy Status: ready --> + <sect1 xml:id="language.oop5.iterations" xmlns="http://docbook.org/ns/docbook"> + <title>对象迭代</title> + <para> + + PHP 5 provides a way for objects to be defined so it is possible to iterate + through a list of items, with, for example a <link + linkend="control-structures.foreach">foreach</link> statement. By default, + all <link linkend="language.oop5.visibility">visible</link> properties will be used + for the iteration. + + PHP5提供了一种迭代(iteration)对象的功能,就像使用数组那样,可以通过<link linkend="control-structures.foreach">foreach</link> + 来遍历对象中的属性。默认情况下,在外部迭代只能得到外部可见的属性的值。 + </para> + + <example> + <title>简单的对象迭代</title> + <programlisting role="php"> +<![CDATA[ +<?php +class MyClass +{ + public $var1 = 'value 1'; + public $var2 = 'value 2'; + public $var3 = 'value 3'; + + protected $protected = 'protected var'; + private $private = 'private var'; + + function iterateVisible() { + echo "MyClass::iterateVisible:\n"; + foreach($this as $key => $value) { + print "$key => $value\n"; + } + } +} + +$class = new MyClass(); + +foreach($class as $key => $value) { + print "$key => $value\n"; +} +echo "\n"; + + +$class->iterateVisible(); + +?> +]]> + </programlisting> + &example.outputs; + <screen role="php"> +<![CDATA[ +var1 => value 1 +var2 => value 2 +var3 => value 3 + +MyClass::iterateVisible: +var1 => value 1 +var2 => value 2 +var3 => value 3 +protected => protected var +private => private var +]]> + </screen> + + </example> + + <para> + 如上所示, the <link + linkend="control-structures.foreach">foreach</link> 遍历了所有 + <link linkend="language.oop5.visibility">可见的</link> 属性. 你也可以通过实现PHP 5自带的 + <varname>Iterator</varname>接口来实现迭代. 使用Iterator接口可以让对象自行决定如何迭代自已。 + </para> + + <example> + <title>实现Iterator接口的迭代</title> + <programlisting role="php"> +<![CDATA[ +<?php +class MyIterator implements Iterator +{ + private $var = array(); + + public function __construct($array) + { + if (is_array($array)) { + $this->var = $array; + } + } + + public function rewind() { + echo "rewinding\n"; + reset($this->var); + } + + public function current() { + $var = current($this->var); + echo "current: $var\n"; + return $var; + } + + public function key() { + $var = key($this->var); + echo "key: $var\n"; + return $var; + } + + public function next() { + $var = next($this->var); + echo "next: $var\n"; + return $var; + } + + public function valid() { + $var = $this->current() !== false; + echo "valid: {$var}\n"; + return $var; + } +} + +$values = array(1,2,3); +$it = new MyIterator($values); + +foreach ($it as $a => $b) { + print "$a: $b\n"; +} +?> +]]> + </programlisting> + &example.outputs; + <screen role="php"> +<![CDATA[ +rewinding +current: 1 +valid: 1 +current: 1 +key: 0 +0: 1 +next: 2 +current: 2 +valid: 1 +current: 2 +key: 1 +1: 2 +next: 3 +current: 3 +valid: 1 +current: 3 +key: 2 +2: 3 +next: +current: +valid: +]]> + </screen> + + </example> + + <para> + 你也可以让类实现<varname>IteratorAggregate</varname>接口,这样你的类就不用强制性地实现 + <varname>Iterator</varname>接口中的所有方法。 + </para> + + <example> + <title>通过IteratorAggregate来实现对象迭代</title> + <programlisting role="php"> +<![CDATA[ +<?php +class MyCollection implements IteratorAggregate +{ + private $items = array(); + private $count = 0; + + // Required definition of interface IteratorAggregate + public function getIterator() { + return new MyIterator($this->items); + } + + public function add($value) { + $this->items[$this->count++] = $value; + } +} + +$coll = new MyCollection(); +$coll->add('value 1'); +$coll->add('value 2'); +$coll->add('value 3'); + +foreach ($coll as $key => $val) { + echo "key/value: [$key -> $val]\n\n"; +} +?> +]]> + </programlisting> + &example.outputs; + <screen role="php"> +<![CDATA[ +rewinding +current: value 1 +valid: 1 +current: value 1 +key: 0 +key/value: [0 -> value 1] + +next: value 2 +current: value 2 +valid: 1 +current: value 2 +key: 1 +key/value: [1 -> value 2] + +next: value 3 +current: value 3 +valid: 1 +current: value 3 +key: 2 +key/value: [2 -> value 3] + +next: +current: +valid: +]]> + </screen> + + </example> + + <note> + <para> + 请查看<link linkend="ref.spl">SPL Extension</link>了解更多Iterator相关信息。 + </para> + </note> + + </sect1> + +<!-- Keep this comment at the end of the file +Local variables: +mode: sgml +sgml-omittag:t +sgml-shorttag:t +sgml-minimize-attributes:nil +sgml-always-quote-attributes:t +sgml-indent-step:1 +sgml-indent-data:t +indent-tabs-mode:nil +sgml-parent-document:nil +sgml-default-dtd-file:"../../manual.ced" +sgml-exposed-tags:nil +sgml-local-catalogs:nil +sgml-local-ecat-files:nil +End: +vim600: syn=xml fen fdm=syntax fdl=2 si +vim: et tw=78 syn=sgml +vi: ts=1 sw=1 +--> http://cvs.php.net/viewvc.cgi/phpdoc-zh/language/oop5/magic.xml?r1=1.2&r2=1.3&diff_format=u Index: phpdoc-zh/language/oop5/magic.xml diff -u /dev/null phpdoc-zh/language/oop5/magic.xml:1.3 --- /dev/null Sat Dec 13 03:45:30 2008 +++ phpdoc-zh/language/oop5/magic.xml Sat Dec 13 03:45:30 2008 @@ -0,0 +1,215 @@ +<?xml version="1.0" encoding="gb2312"?> +<!-- $Revision: 1.3 $ --> +<!-- $Author: haohappy $ --> +<!-- EN-Revision: 1.20 Maintainer: haohappy Status: ready --> + <sect1 xml:id="language.oop5.magic" xmlns="http://docbook.org/ns/docbook"> + <title>魔术方法</title> + <para> + <literal>__construct</literal>, + <literal>__destruct</literal> + (参看 <link linkend="language.oop5.decon">构造方法和析构方法</link>), + <literal>__call</literal>, + <literal>__callStatic</literal>, + <literal>__get</literal>, + <literal>__set</literal>, + <literal>__isset</literal>, + <literal>__unset</literal> + (参看 <link linkend="language.oop5.overloading">重载</link>), + <literal>__sleep</literal>, + <literal>__wakeup</literal>, + <literal>__toString</literal>, + <literal>__set_state</literal> 和 + <link linkend="language.oop5.cloning">__clone</link> + 等方法在PHP中被称为“魔术方法”(Magic methods)。 你在命名自己的类方法时不能使用这些方法名。 + </para> + + <caution> + <simpara> + PHP把所有以__(两个下划线)开头的类方法当成魔术方法。所以你定义自己的类方法时,不要以 + __为前缀。 + </simpara> + </caution> + + <sect2 xml:id="language.oop5.magic.sleep"> + <title><literal>__sleep</literal> 和 <literal>__wakeup</literal></title> + <para> + <function>serialize</function> 函数会检查是否存在一个魔术方法 <literal>__sleep</literal>.如果存在,__sleep()方法会先被调用, + 然后才执行序列化操作。这个功能可以用于清理对象,并返回一个包含对象中所有变量名称的数组。如果该方法不返回任何内容,则&null;被序列化,导致 + 一个E_NOTICE错误。 + </para> + <para> + <literal>__sleep</literal>方法常用于提交未提交的数据,或类似的操作。同时,如果你有一些很大的对象, +不需要保存,这个功能就很好用。 + + </para> + <para> + 与之相反,<function>unserialize</function>会检查是否存在一个<literal>__wakeup</literal>方法。如果存在,则会先调用 +<literal>__wakeup</literal>方法,预先准备对象数据。 + </para> + <para> + <literal>__wakeup</literal>经常用在反序列化操作中,例如重新建立数据库连接,或执行其它初始化操作。 + </para> + <example> + <title>Sleep 和 wakeup</title> + <programlisting role="php"> +<![CDATA[ +<?php +class Connection { + protected $link; + private $server, $username, $password, $db; + + public function __construct($server, $username, $password, $db) + { + $this->server = $server; + $this->username = $username; + $this->password = $password; + $this->db = $db; + $this->connect(); + } + + private function connect() + { + $this->link = mysql_connect($this->server, $this->username, $this->password); + mysql_select_db($this->db, $this->link); + } + + public function __sleep() + { + return array('server', 'username', 'password', 'db'); + } + + public function __wakeup() + { + $this->connect(); + } +} +?> +]]> + </programlisting> + </example> + </sect2> + + <sect2 xml:id="language.oop5.magic.tostring"> + <title><literal>__toString</literal></title> + <para> + The <literal>__toString</literal> method allows a class to decide + how it will react when it is converted to a string. + + <literal>__toString</literal> 方法可以让一个类决定它如何转换成一个字符串。 + </para> + <example> + <title>简单示例</title> + <programlisting role="php"> +<![CDATA[ +<?php +// Declare a simple class +class TestClass +{ + public $foo; + + public function __construct($foo) { + $this->foo = $foo; + } + + public function __toString() { + return $this->foo; + } +} + +$class = new TestClass('Hello'); +echo $class; +?> +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +Hello +]]> + </screen> + </example> + <para> + 在PHP 5.2.0之前,<literal>__toString</literal>方法只有结合使用<function>echo</function> 或 <function>print</function>时 +才能生效。PHP 5.2.0之后,则可以在任何字符串环境生效(例如通过<function>printf</function>,使用<literal>%s</literal>修饰符),但 +不能用于非字符串环境(如使用<literal>%d</literal>修饰符)。从PHP 5.2.0,如果将一个未定义<literal>__toString</literal>方法的对象 +转换为字符串,会报出一个<constant>E_RECOVERABLE_ERROR</constant>错误。 + + </para> + </sect2> + + <sect2 xml:id="language.oop5.magic.set-state"> + <title><literal>__set_state</literal></title> + <para> + 当调用<function>var_export</function>时,这个<link linkend="language.oop5.static">静态</link> 方法会被调用(自PHP 5.1.0起有效)。 + </para> + <para> + 本方法的唯一参数是一个数组,其中包含按<literal>array('property' => value, ...)</literal>格式排列的类属性。 + </para> + <example> + <title>Using <literal>__set_state</literal> (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> + +<!-- Keep this comment at the end of the file +Local variables: +mode: sgml +sgml-omittag:t +sgml-shorttag:t +sgml-minimize-attributes:nil +sgml-always-quote-attributes:t +sgml-indent-step:1 +sgml-indent-data:t +indent-tabs-mode:nil +sgml-parent-document:nil +sgml-default-dtd-file:"../../manual.ced" +sgml-exposed-tags:nil +sgml-local-catalogs:nil +sgml-local-ecat-files:nil +End: +vim600: syn=xml fen fdm=syntax fdl=2 si +vim: et tw=78 syn=sgml +vi: ts=1 sw=1 +--> http://cvs.php.net/viewvc.cgi/phpdoc-zh/language/oop5/object-comparison.xml?r1=1.3&r2=1.4&diff_format=u Index: phpdoc-zh/language/oop5/object-comparison.xml diff -u /dev/null phpdoc-zh/language/oop5/object-comparison.xml:1.4 --- /dev/null Sat Dec 13 03:45:30 2008 +++ phpdoc-zh/language/oop5/object-comparison.xml Sat Dec 13 03:45:30 2008 @@ -0,0 +1,125 @@ +<?xml version="1.0" encoding="gb2312"?> +<!-- $Revision: 1.4 $ --> +<!-- $Author: haohappy $ --> +<!-- EN-Revision: 1.6 Maintainer: haohappy Status: ready --> + <sect1 xml:id="language.oop5.object-comparison" xmlns="http://docbook.org/ns/docbook"> + <title>对象比较</title> + <para> + PHP5中的对象比较(object comparison)要比PHP4中复杂,也比其它一般的面向对象语言复杂。 + </para> + <para> + 当使用对比操作符(<literal>==</literal>)比较两个对象变量时,比较的原则是:如果两个对象的属性和属性值 + 都相等,而且两个对象是同一个类的实例,那么这两个对象变量相等。 + </para> + <para> + 而如果使用全等操作符(<literal>===</literal>),这两个对象变量一定要指向某个类的同一个实例(即同一个对象)。 + </para> + <para> + 通过下面的示例你可以理解以上原则。 + <example> + <title>PHP 5的对象比较</title> + <programlisting role='php'> +<![CDATA[ +<?php +function bool2str($bool) +{ + if ($bool === false) { + return 'FALSE'; + } else { + return 'TRUE'; + } +} + +function compareObjects(&$o1, &$o2) +{ + echo 'o1 == o2 : ' . bool2str($o1 == $o2) . "\n"; + echo 'o1 != o2 : ' . bool2str($o1 != $o2) . "\n"; + echo 'o1 === o2 : ' . bool2str($o1 === $o2) . "\n"; + echo 'o1 !== o2 : ' . bool2str($o1 !== $o2) . "\n"; +} + +class Flag +{ + public $flag; + + function Flag($flag = true) { + $this->flag = $flag; + } +} + +class OtherFlag +{ + public $flag; + + function OtherFlag($flag = true) { + $this->flag = $flag; + } +} + +$o = new Flag(); +$p = new Flag(); +$q = $o; +$r = new OtherFlag(); + +echo "Two instances of the same class\n"; +compareObjects($o, $p); + +echo "\nTwo references to the same instance\n"; +compareObjects($o, $q); + +echo "\nInstances of two different classes\n"; +compareObjects($o, $r); +?> +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +Two instances of the same class +o1 == o2 : TRUE +o1 != o2 : FALSE +o1 === o2 : FALSE +o1 !== o2 : TRUE + +Two references to the same instance +o1 == o2 : TRUE +o1 != o2 : FALSE +o1 === o2 : TRUE +o1 !== o2 : FALSE + +Instances of two different classes +o1 == o2 : FALSE +o1 != o2 : TRUE +o1 === o2 : FALSE +o1 !== o2 : TRUE +]]> + </screen> +</example> +</para> +<note> +<para> +PHP扩展中可以自行定义对象比较的原则。 +</para> +</note> +</sect1> + +<!-- Keep this comment at the end of the file +Local variables: +mode: sgml +sgml-omittag:t +sgml-shorttag:t +sgml-minimize-attributes:nil +sgml-always-quote-attributes:t +sgml-indent-step:1 +sgml-indent-data:t +indent-tabs-mode:nil +sgml-parent-document:nil +sgml-default-dtd-file:"../../manual.ced" +sgml-exposed-tags:nil +sgml-local-catalogs:nil +sgml-local-ecat-files:nil +End: +vim600: syn=xml fen fdm=syntax fdl=2 si +vim: et tw=78 syn=sgml +vi: ts=1 sw=1 +--> http://cvs.php.net/viewvc.cgi/phpdoc-zh/language/oop5/overloading.xml?r1=1.3&r2=1.4&diff_format=u Index: phpdoc-zh/language/oop5/overloading.xml diff -u /dev/null phpdoc-zh/language/oop5/overloading.xml:1.4 --- /dev/null Sat Dec 13 03:45:30 2008 +++ phpdoc-zh/language/oop5/overloading.xml Sat Dec 13 03:45:30 2008 @@ -0,0 +1,315 @@ +<?xml version="1.0" encoding="gb2312"?> +<!-- $Revision: 1.4 $ --> +<!-- $Author: haohappy $ --> +<!-- EN-Revision: 1.32 Maintainer: haohappy Status: ready --> + <sect1 xml:id="language.oop5.overloading" xmlns="http://docbook.org/ns/docbook"> + <title>重载</title> + + <para> + PHP所提供的<quote>重载</quote>(overloading)是指动态地<quote>创建</quote>类属性和方法。我们是通过 + 魔术方法(magic methods)来实现的。 + </para> + + <para> + 当调用当前环境下未定义或不可见的类属性或方法时,重载方法会被调用。本节后面将使用 + <quote>不可访问成员(inaccessible members)</quote>和<quote>不可访问方法(inaccessible + methods)</quote>来称呼这些未定义或不可见的类属性或方法。 + </para> + + <para> + 所有的重载方法都必须被声明为<literal>public</literal>。 + </para> + + <note> + <para> + 这些魔术方法的参数都不能 <link linkend="functions.arguments.by-reference">通过引用传递</link>。 + </para> + </note> + + <note> + <para> + PHP中的<quote>重载</quote>与其它绝大多数面向对象语言不同。传统的<quote>重载</quote>是用于提供多个同名的 + 类方法,但各方法的参数类型和个数不同。 + </para> + </note> + + + <sect2 xml:id="language.oop5.overloading.changelog"> + &reftitle.changelog; + <para> + <informaltable> + <tgroup cols="2"> + <thead> + <row> + <entry>&Version;</entry> + <entry>&Description;</entry> + </row> + </thead> + <tbody> + <row> + <entry>5.1.0</entry> + <entry> + 新增 <literal>__isset()</literal> 和 <literal>__unset()</literal>两个魔术方法. + </entry> + </row> + <row> + <entry>5.3.0</entry> + <entry> + 新增 <literal>__callStatic()</literal>魔术方法. + 可见性未设置为public或未声明为static的时候会产生一个警告 + </entry> + </row> + </tbody> + </tgroup> + </informaltable> + </para> + </sect2> + + + <sect2 xml:id="language.oop5.overloading.members"> + <title>属性重载</title> + + <methodsynopsis> + <type>void</type><methodname>__set</methodname> + <methodparam><type>string</type><parameter>name</parameter></methodparam> + <methodparam><type>mixed</type><parameter>value</parameter></methodparam> + </methodsynopsis> + <methodsynopsis> + <type>mixed</type><methodname>__get</methodname> + <methodparam><type>string</type><parameter>name</parameter></methodparam> + </methodsynopsis> + <methodsynopsis> + <type>bool</type><methodname>__isset</methodname> + <methodparam><type>string</type><parameter>name</parameter></methodparam> + </methodsynopsis> + <methodsynopsis> + <type>void</type><methodname>__unset</methodname> + <methodparam><type>string</type><parameter>name</parameter></methodparam> + </methodsynopsis> + + <para> + 在给未定义的变量赋值时,<literal>__set()</literal> 会被调用。 + </para> + + <para> + 读取未定义的变量的值时,<literal>__get()</literal> 会被调用。 + </para> + + <para> + 当对未定义的变量调用<function>isset</function> 或 <function>empty</function>时,<literal>__isset()</literal> + 会被调用。 + </para> + + <para> + 当对未定义的变量调用<function>unset</function>时,<literal>__unset()</literal> + 会被调用。 + </para> + + <para> + The <varname>$name</varname> argument is the name of the + member being interacted with. The <literal>__set()</literal> + method's <varname>$value</varname> argument specifies the + value the <varname>$name</varname>'ed member should be set + to. + + 参数<varname>$name</varname>是指要操作的变量名称。<literal>__set()</literal> + 方法的<varname>$value</varname> 参数指定了<varname>$name</varname>变量的值。 + </para> + + <para> + 属性重载只能在对象中进行。在静态方法中,这些魔术方法将不会被调用。所以这些方法都不能被 + 声明为<link linkend="language.oop5.static">static</link>。 + </para> + + <example> + <title>使用__get, __set, __isset 和 __unset 进行重载</title> + <programlisting role="php"> +<![CDATA[ +<?php +class MemberTest { + /** 被重载的数据保存在此 */ + private $data = array(); + + + /** 重载不能被用在已经定义的属性 */ + public $declared = 1; + + /** 只有从类外部访问这个属性时,重载才会发生 */ + private $hidden = 2; + + public function __set($name, $value) { + echo "Setting '$name' to '$value'\n"; + $this->data[$name] = $value; + } + + public function __get($name) { + echo "Getting '$name'\n"; + if (array_key_exists($name, $this->data)) { + return $this->data[$name]; + } + + $trace = debug_backtrace(); + trigger_error( + 'Undefined property via __get(): ' . $name . + ' in ' . $trace[0]['file'] . + ' on line ' . $trace[0]['line'], + E_USER_NOTICE); + return null; + } + + /** PHP 5.1.0之后版本 */ + public function __isset($name) { + echo "Is '$name' set?\n"; + return isset($this->data[$name]); + } + + /** PHP 5.1.0之后版本 */ + public function __unset($name) { + echo "Unsetting '$name'\n"; + unset($this->data[$name]); + } + + /** 非魔术方法 */ + public function getHidden() { + return $this->hidden; + } +} + + +echo "<pre>\n"; + +$obj = new MemberTest; + +$obj->a = 1; +echo $obj->a . "\n\n"; + +var_dump(isset($obj->a)); +unset($obj->a); +var_dump(isset($obj->a)); +echo "\n"; + +echo $obj->declared . "\n\n"; + +echo "Let's experiment with the private property named 'hidden':\n"; +echo "Privates are visible inside the class, so __get() not used...\n"; +echo $obj->getHidden() . "\n"; +echo "Privates not visible outside of class, so __get() is used...\n"; +echo $obj->hidden . "\n"; +?> +]]> + </programlisting> + &example.outputs; + <screen role="php"> +<![CDATA[ +Setting 'a' to '1' +Getting 'a' +1 + +Is 'a' set? +bool(true) +Unsetting 'a' +Is 'a' set? +bool(false) + +1 + +Let's experiment with the private property named 'hidden': +Privates are visible inside the class, so __get() not used... +2 +Privates not visible outside of class, so __get() is used... +Getting 'hidden' + + +Notice: Undefined property via __get(): hidden in <file> on line 70 in <file> on line 29 +]]> + </screen> + + </example> + </sect2> + + <sect2 xml:id="language.oop5.overloading.methods"> + <title>方法重载</title> + + <methodsynopsis> + <type>mixed</type><methodname>__call</methodname> + <methodparam><type>string</type><parameter>name</parameter></methodparam> + <methodparam><type>array</type><parameter>arguments</parameter></methodparam> + </methodsynopsis> + <methodsynopsis> + <type>mixed</type><methodname>__callStatic</methodname> + <methodparam><type>string</type><parameter>name</parameter></methodparam> + <methodparam><type>array</type><parameter>arguments</parameter></methodparam> + </methodsynopsis> + + <para> + 当调用一个不可访问方法(如未定义,或者不可见)时,<literal>__call()</literal> 会被调用。 + </para> + + <para> + 当在静态方法中调用一个不可访问方法(如未定义,或者不可见)时,<literal>__callStatic()</literal> 会被调用。 + </para> + + <para> +<varname>$name</varname>参数是要调用的方法名称。<varname>$arguments</varname>参数是一个数组,包含着要传递给方法的参数。 + </para> + + <example> + <title>使用 __call 和 ___callStatic 调用对象中的方法</title> + <programlisting role="php"> + <![CDATA[ +<?php +class MethodTest { + public function __call($name, $arguments) { + // 注意: $name 区分大小写 + echo "Calling object method '$name' " + . implode(', ', $arguments). "\n"; + } + + /** PHP 5.3.0之后版本 */ + public static function __callStatic($name, $arguments) { + // Note: value of $name is case sensitive. + echo "Calling static method '$name' " + . implode(', ', $arguments). "\n"; + } +} + +$obj = new MethodTest; +$obj->runTest('in object context'); + +MethodTest::runTest('in static context'); // PHP 5.3.0之后版本 +?> +]]> + </programlisting> + &example.outputs; + <screen role="php"> +<![CDATA[ +Calling object method 'runTest' in object context +Calling static method 'runTest' in static context +]]> + </screen> + </example> + + </sect2> + + </sect1> + +<!-- Keep this comment at the end of the file +Local variables: +mode: sgml +sgml-omittag:t +sgml-shorttag:t +sgml-minimize-attributes:nil +sgml-always-quote-attributes:t +sgml-indent-step:1 +sgml-indent-data:t +indent-tabs-mode:nil +sgml-parent-document:nil +sgml-default-dtd-file:"../../manual.ced" +sgml-exposed-tags:nil +sgml-local-catalogs:nil +sgml-local-ecat-files:nil +End: +vim600: syn=xml fen fdm=syntax fdl=2 si +vim: et tw=78 syn=sgml +vi: ts=1 sw=1 +-->
« previous php.doc.zh (#3048) next »