cvs: phpdoc-zh / translation.xml /language oop.xml
| From: | Dallas Thunder | Date: | Sat, 08 Mar 2003 09:19:58 +0000 |
| Subject: | cvs: phpdoc-zh / translation.xml /language oop.xml | ||
| Groups: | php.doc.zh | ||
| Request: | Send a blank email to doc-zh+get-1188@lists.php.net to get a copy of this message | ||
dallas Sat Mar 8 04:19:58 2003 EDT
Modified files:
/phpdoc-zh translation.xml
/phpdoc-zh/language oop.xml
Log:
oop.xml done.
Index: phpdoc-zh/translation.xml diff -u phpdoc-zh/translation.xml:1.92 phpdoc-zh/translation.xml:1.93 --- phpdoc-zh/translation.xml:1.92 Thu Mar 6 09:19:57 2003 +++ phpdoc-zh/translation.xml Sat Mar 8 04:19:57 2003 @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="gb2312" ?> <!DOCTYPE translation SYSTEM "../dtds/translation.dtd"> -<!-- $Id: translation.xml,v 1.92 2003/03/06 14:19:57 dallas Exp $ --> +<!-- $Id: translation.xml,v 1.93 2003/03/08 09:19:57 dallas Exp $ --> <translation> <intro> @@ -36,8 +36,6 @@ ×¢:·ÒëÍê³ÉµÄÎļþÇë´ÓÁбíÖÐɾ³ý. --> - <file name="language/oop.xml" person="verdana" type="working" revision="1.36" date="15/08/2002" /> - <file name="chapters/security.xml" person="Gregory" type="working" revision="1.49" date="25/12/2002" /> <file name="appendices/phpdevel.xml" person="class007" type="working" /> Index: phpdoc-zh/language/oop.xml diff -u phpdoc-zh/language/oop.xml:1.6 phpdoc-zh/language/oop.xml:1.7 --- phpdoc-zh/language/oop.xml:1.6 Mon Sep 16 00:50:58 2002 +++ phpdoc-zh/language/oop.xml Sat Mar 8 04:19:57 2003 @@ -1,7 +1,8 @@ -<?xml version="1.0" encoding="iso-8859-1"?> -<!-- $Revision: 1.6 $ --> -<!-- $Author: gregory $--> -<!-- EN-Revision: 1.37 Maintainer: verdana Status:partly --> +<?xml version="1.0" encoding="gb2312"?> +<!-- $Revision: 1.7 $ --> +<!-- $Author: dallas $--> +<!-- EN-Revision: 1.37 Maintainer: verdana Status: ready --> +<!-- CREDITS: dallas --> <chapter id="language.oop"> <title>ÀàÓë¶ÔÏó</title> @@ -15,16 +16,16 @@ <?php class Cart { - var $items; // Items in our shopping cart + var $items; // ¹ºÎï³µÖеÄÏîÄ¿ - // Add $num articles of $artnr to the cart + // °Ñ $num ¸ö $artnr ·ÅÈë³µÖÐ function add_item ($artnr, $num) { $this->items[$artnr] += $num; } - // Take $num articles of $artnr out of the cart + // °Ñ $num ¸ö $artnr ´Ó³µÖÐÈ¡³ö function remove_item ($artnr, $num) { @@ -43,46 +44,42 @@ </para> <para> - ÉÏÃæµÄÀý×Ó¶¨ÒåÁËÒ»¸ö Cart À࣬Õâ¸öÀàÓɹºÎï³µÖеÄÉÌÆ·¹¹³ÉµÄÊý×éºÍÁ½¸öÓÃÓÚ - ´Ó¹ºÎï³µÖÐÌí¼ÓºÍɾ³ýÉÌÆ·µÄº¯Êý×é³É¡£ + ÉÏÃæµÄÀý×Ó¶¨ÒåÁËÒ»¸ö Cart + À࣬Õâ¸öÀàÓɹºÎï³µÖеÄÉÌÆ·¹¹³ÉµÄÊý×éºÍÁ½¸öÓÃÓÚ´Ó¹ºÎï³µÖÐÌí¼ÓºÍɾ³ýÉÌÆ·µÄº¯Êý×é³É¡£ </para> <caution> <simpara> - ÏÂÃæµÄ¾¯¸æ×¢½â¶Ô PHP4 ÓÐЧ¡£ + ÏÂÃæµÄ¾¯¸æ×¢½â¶Ô PHP4 ÓÐЧ¡£ </simpara> <simpara> - Ãû³Æ <literal>stdClass</literal> ÒѾ±» Zend ʹÓò¢±£Áô¡£Äú²»ÄÜÔÚÄúµÄ PHP ´úÂë - Öж¨ÒåÃûΪ <literal>stdClass</literal> µÄÀà¡£ + Ãû³Æ <literal>stdClass</literal> ÒѾ±» Zend ʹÓò¢±£Áô¡£Äú²»ÄÜÔÚÄúµÄ PHP + ´úÂëÖж¨ÒåÃûΪ <literal>stdClass</literal> µÄÀà¡£ </simpara> <simpara> - The function names <literal>__sleep</literal> and - <literal>__wakeup</literal> are magical in PHP classes. You - cannot have functions with these names in any of your - classes unless you want the magic functionality associated - with them. See below for more information. + º¯ÊýÃû <literal>__sleep</literal> ºÍ + <literal>__wakeup</literal> ÔÚ + PHP ÀàÖÐÊÇħÊõº¯Êý¡£³ý·ÇÏëÒªÓëÖ®ÁªÏµµÄħÊõ¹¦ÄÜ£¬·ñÔòÔÚÈκÎÀàÖж¼²»ÄÜÒÔ´ËÃüÃûº¯Êý¡£ </simpara> <simpara> - PHP reserves all function names starting with __ as magical. - It is recommended that you do not use function names with - __ in PHP unless you want some documented magic functionality. + PHP ½«ËùÓÐÒÔ __ ¿ªÍ·µÄº¯ÊýÃû±£ÁôΪħÊõº¯Êý¡£³ý·ÇÏëҪʹÓÃһЩ¼ûÓÚÎĵµÖеÄħÊõ¹¦ÄÜ£¬·ñÔò½¨Òé²»ÒªÔÚ + PHP Öн«º¯ÊýÃûÒÔ __ ¿ªÍ·¡£ </simpara> </caution> <note> <simpara> - ÔÚ PHP 4 ÖУ¬<literal>var</literal> ±äÁ¿µÄÖµÖ»Äܳõʼ»¯Îª³£Á¿¡£Ó÷dz£Á¿Öµ³õʼ»¯±äÁ¿£¬ - ÄúÐèÒªÒ»¸ö³õʼ»¯º¯Êý£¬¸Ãº¯ÊýÔÚ¶ÔÏó±»´´½¨Ê±×Ô¶¯±»µ÷Óá£ÕâÑùÒ»¸öº¯Êý±»³ÆÖ®Îª¹¹Ô캯Êý - £¨¼ûÏÂÃæ£©¡£ + ÔÚ PHP 4 ÖУ¬<literal>var</literal> + ±äÁ¿µÄÖµÖ»Äܳõʼ»¯Îª³£Á¿¡£Ó÷dz£Á¿Öµ³õʼ»¯±äÁ¿£¬ÄúÐèÒªÒ»¸ö³õʼ»¯º¯Êý£¬¸Ãº¯ÊýÔÚ¶ÔÏó±»´´½¨Ê±×Ô¶¯±»µ÷Óá£ÕâÑùÒ»¸öº¯Êý±»³ÆÖ®Îª¹¹Ô캯Êý£¨¼ûÏÂÃæ£©¡£ </simpara> <informalexample> <programlisting role="php"> <![CDATA[ <?php -/* None of these will work in PHP 4. */ +/* PHP 4 Öв»ÄÜÕâÑùÓà */ class Cart { var $todays_date = date("Y-m-d"); @@ -91,7 +88,7 @@ var $items = array("VCR", "TV"); } -/* This is how it should be done. */ +/* Ó¦¸ÃÕâÑù½øÐÐ */ class Cart { var $todays_date; @@ -113,9 +110,8 @@ </note> <para> - Classes are types, that is, they are blueprints for actual - variables. You have to create a variable of the desired type with - the <literal>new</literal> operator. + ÀàÒ²ÊÇÒ»ÖÖÀàÐÍ£¬¾ÍÊÇ˵£¬ËüÃÇÊÇʵ¼Ê±äÁ¿µÄÀ¶Í¼¡£±ØÐëÓà + <literal>new</literal> ÔËËã·ûÀ´´´½¨ÏàÓ¦ÀàÐ͵ıäÁ¿¡£ </para> <informalexample> @@ -133,31 +129,30 @@ <para> ÉÏÊö´úÂë´´½¨ÁËÁ½¸ö Cart ÀàµÄ¶ÔÏó $cart ºÍ $another_cart, ¶ÔÏó $cart µÄ·½·¨ add_item() - ±»µ÷ÓÃʱ£¬Ìí¼ÓÁË1¼þ10ºÅÉÌÆ·¡£¶ÔÓÚ¶ÔÏó $another_cart, 3¼þ0815ºÅÉÌÆ·±»Ìí¼Óµ½¹ºÎï³µÖС£ + ±»µ÷ÓÃʱ£¬Ìí¼ÓÁË 1 ¼þ 10 ºÅÉÌÆ·¡£¶ÔÓÚ¶ÔÏó $another_cart, 3 ¼þ 0815 ºÅÉÌÆ·±»Ìí¼Óµ½¹ºÎï³µÖС£ </para> <para> - $cart ºÍ $another_cart ¶¼Óз½·¨ add_item(), remove_item() ºÍÒ»¸ö items ±äÁ¿¡£ - ËüÃǶ¼ÊÇÃ÷ÏԵĺ¯ÊýºÍ±äÁ¿¡£Äã¿ÉÒÔ°ÑËüÃǵ±×÷ÎļþϵͳÖеÄijЩÀàËÆÄ¿Â¼µÄ¶«Î÷À´¿¼ÂÇ¡£ - ÔÚÎļþϵͳÖУ¬Äã¿ÉÒÔÓµÓÐÁ½¸ö²»Í¬µÄ README.TXT Îļþ£¬Ö»Òª²»ÔÚÏàͬµÄĿ¼ÖС£ - ÕýÈç´ÓΪÁ˸ùĿ¼·ÃÎÊÿ¸öÎļþÄãÐèÒªÊäÈë¸ÃÎļþµÄÍêÕûµÄ·¾¶ÃûÒ»Ñù£¬Äã±ØÐëÖ¸¶¨Ðè - Òªµ÷Óõĺ¯ÊýµÄÍêÕûÃû³Æ£ºÔÚ PHP ÊõÓïÖУ¬¸ùĿ¼½«ÊÇÈ«¾ÖÃû³Æ¿Õ¼ä£¬Â·¾¶Ãû·ûºÅ½«ÊÇ - ->¡£Òò¶ø£¬Ãû³Æ $cart->items ºÍ $another_cart->items ÃüÃûÁËÁ½¸ö²»Í¬µÄ - ±äÁ¿¡£×¢Òâ±äÁ¿ $cart->items£¬²»ÊÇ $cart->$items£¬ÄÇÊÇÒòΪÔÚ PHP ÖÐÒ»¸ö±äÁ¿ - ÃûÖ»ÓÐÒ»¸öµ¥¶ÀµÄÃÀÔª·ûºÅ¡£ + $cart ºÍ $another_cart ¶¼Óз½·¨ add_item(), remove_item() ºÍÒ»¸ö items + ±äÁ¿¡£ËüÃǶ¼ÊÇÃ÷ÏԵĺ¯ÊýºÍ±äÁ¿¡£Äã¿ÉÒÔ°ÑËüÃǵ±×÷ÎļþϵͳÖеÄijЩÀàËÆÄ¿Â¼µÄ¶«Î÷À´¿¼ÂÇ¡£ÔÚÎļþϵͳÖУ¬Äã¿ÉÒÔÓµÓÐÁ½¸ö²»Í¬µÄ + README.TXT Îļþ£¬Ö»Òª²»ÔÚÏàͬµÄĿ¼ÖС£ÕýÈç´ÓΪÁ˸ùĿ¼·ÃÎÊÿ¸öÎļþÄãÐèÒªÊäÈë¸ÃÎļþµÄÍêÕûµÄ·¾¶ÃûÒ»Ñù£¬Äã±ØÐëÖ¸¶¨ÐèÒªµ÷Óõĺ¯ÊýµÄÍêÕûÃû³Æ£ºÔÚ + PHP ÊõÓïÖУ¬¸ùĿ¼½«ÊÇÈ«¾ÖÃû³Æ¿Õ¼ä£¬Â·¾¶Ãû·ûºÅ½«ÊÇ + ->¡£Òò¶ø£¬Ãû³Æ $cart->items ºÍ $another_cart->items + ÃüÃûÁËÁ½¸ö²»Í¬µÄ±äÁ¿¡£×¢Òâ±äÁ¿ $cart->items£¬²»ÊÇ + $cart->$items£¬ÄÇÊÇÒòΪÔÚ PHP ÖÐÒ»¸ö±äÁ¿ÃûÖ»ÓÐÒ»¸öµ¥¶ÀµÄÃÀÔª·ûºÅ¡£ </para> <informalexample> <programlisting role="php"> <![CDATA[ -// correct, single $ +// ÕýÈ·£¬Ö»ÓÐÒ»¸ö $ $cart->items = array("10" => 1); -// invalid, because $cart->$items becomes $cart->"" +// ²»ÕýÈ·£¬ÒòΪ $cart->$items ±ä³ÉÁË $cart->"" $cart->$items = array("10" => 1); -// correct, but may or may not be what was intended: -// $cart->$myvar becomes $cart->items +// ÕýÈ·£¬µ«¿ÉÄܲ»ÊÇÏëÒªµÄ½á¹û£º +// $cart->$myvar ±ä³ÉÁË $cart->items $myvar = 'items'; $cart->$myvar = array("10" => 1); ]]> @@ -165,18 +160,17 @@ </informalexample> <para> - ÔÚÒ»¸öÀàµÄ¶¨ÒåÄÚ²¿£¬ÄãÎÞ·¨µÃ֪ʹÓúÎÖÖÃû³ÆµÄ¶ÔÏóÊÇ¿ÉÒÔ·ÃÎʵģºÔÚ±àд Cart Ààʱ£¬²¢ - ²»ÖªµÀÖ®ºó¶ÔÏóµÄÃû³Æ½«»áÃüÃûΪ $cart »òÕß $another_cart¡£Òò¶øÄã²»ÄÜÔÚÀàÖÐʹÓà - $cart->items¡£È»¶øÎªÁËÀඨÒåµÄÄÚ²¿·ÃÎÊ×ÔÉíµÄº¯ÊýºÍ±äÁ¿£¬ ¿ÉÒÔʹÓÃα±äÁ¿ $this - À´´ïµ½Õâ¸öÄ¿µÄ¡£$this ±äÁ¿¿ÉÒÔÀí½âΪ¡®ÎÒ×Ô¼ºµÄ¡¯»òÕß¡®µ±Ç°¶ÔÏ󡯡£Òò¶ø - ¡®$this->>items[$artnr] += $num¡¯ ¿ÉÒÔÀí½âΪ¡®ÎÒ×Ô¼ºµÄÎïÆ·Êý×éµÄ $artnr ¼ÆÊýÆ÷¼Ó $num¡¯ - »òÕß ¡®ÔÚµ±Ç°¶ÔÏóµÄÎïÆ·Êý×éµÄ $artnr ¼ÆÊýÆ÷¼Ó $num¡¯¡£ + ÔÚÒ»¸öÀàµÄ¶¨ÒåÄÚ²¿£¬ÄãÎÞ·¨µÃ֪ʹÓúÎÖÖÃû³ÆµÄ¶ÔÏóÊÇ¿ÉÒÔ·ÃÎʵģºÔÚ±àд Cart + Ààʱ£¬²¢²»ÖªµÀÖ®ºó¶ÔÏóµÄÃû³Æ½«»áÃüÃûΪ $cart »òÕß $another_cart¡£Òò¶øÄã²»ÄÜÔÚÀàÖÐʹÓà + $cart->items¡£È»¶øÎªÁËÀඨÒåµÄÄÚ²¿·ÃÎÊ×ÔÉíµÄº¯ÊýºÍ±äÁ¿£¬ ¿ÉÒÔʹÓÃα±äÁ¿ $this + À´´ïµ½Õâ¸öÄ¿µÄ¡£$this ±äÁ¿¿ÉÒÔÀí½âΪ¡°ÎÒ×Ô¼ºµÄ¡±»òÕß¡°µ±Ç°¶ÔÏ󡱡£Òò¶ø + '$this->>items[$artnr] += $num' ¿ÉÒÔÀí½âΪ¡°ÎÒ×Ô¼ºµÄÎïÆ·Êý×éµÄ $artnr ¼ÆÊýÆ÷¼Ó + $num¡±»òÕß¡°ÔÚµ±Ç°¶ÔÏóµÄÎïÆ·Êý×éµÄ $artnr ¼ÆÊýÆ÷¼Ó $num¡±¡£ </para> <note> <para> - ÓÐһЩ²»´íµÄº¯ÊýÓÃÀ´´¦ÀíÀàºÍ¶ÔÏó¡£ÄãÓ¦¸Ã¹Ø×¢Ò»Ï - <link linkend="ref.classobj">Àà/¶ÔÏó º¯Êý</link>¡£ + ÓÐһЩ²»´íµÄº¯ÊýÓÃÀ´´¦ÀíÀàºÍ¶ÔÏó¡£ÄãÓ¦¸Ã¹Ø×¢Ò»ÏÂ<link linkend="ref.classobj">Àà/¶ÔÏóº¯Êý¿â</link>¡£ </para> </note> </sect1> @@ -185,13 +179,7 @@ <title><literal>¼Ì³Ð</literal></title> <para> - ͨ³£ÄãÐèÒªÕâÑùһЩÀ࣬ÕâЩÀàÓëÆäËüÏÖÓеÄÀàÓµÓÐÏàͬ±äÁ¿ºÍº¯Êý¡£Êµ¼ÊÉÏ£¬¶¨ÒåÒ»¸ö - ͨÓÃÀ࣬ÓÃÓÚÄãËùÓеÄÏîÄ¿£¬²¢ÇÒ²»¶Ï·á¸»Õâ¸öÀàÒÔÊÊÓ¦ÄãµÄÿ¸ö¾ßÌåÏîÄ¿£¬½«ÊÇÒ»¸ö²»´í - µÄÁ·Ï°¡£ÎªÁËʹÕâÒ»µã±äµÃ¸ü¼ÓÈÝÒ×£¬Àà¿ÉÒÔ´ÓÆäËüµÄÀàÖÐÀ©Õ¹³öÀ´¡£À©Õ¹»òÅÉÉú³öÀ´µÄÀà - ÓµÓÐÆä»ùÀࣨthis is called 'inheritance' despite the fact that nobody died£© - µÄËùÓбäÁ¿ºÍº¯Êý£¬²¢°üº¬ËùÓÐÄãÔÚÅÉÉúÀàÖж¨ÒåµÄ²¿·Ö¡£ÀàÖеÄÔªËØ²»¿ÉÄܼõÉÙ£¬¾ÍÊÇ˵£¬ - ²»¿ÉÒÔ×¢ÏúÈκδæÔڵĺ¯Êý»òÕß±äÁ¿¡£Ò»¸öÀ©³äÀà×ÜÊÇÒÀÀµÒ»¸öµ¥¶ÀµÄ»ùÀ࣬Ҳ¾ÍÊÇ˵£¬ - ¶à¼Ì³ÐÊDz»Ö§³ÖµÄ¡£Ê¹Óùؼü×Ö¡®extends¡¯À´À©Õ¹Ò»¸öÀà¡£ + ͨ³£ÄãÐèÒªÕâÑùһЩÀ࣬ÕâЩÀàÓëÆäËüÏÖÓеÄÀàÓµÓÐÏàͬ±äÁ¿ºÍº¯Êý¡£Êµ¼ÊÉÏ£¬¶¨ÒåÒ»¸öͨÓÃÀ࣬ÓÃÓÚÄãËùÓеÄÏîÄ¿£¬²¢ÇÒ²»¶Ï·á¸»Õâ¸öÀàÒÔÊÊÓ¦ÄãµÄÿ¸ö¾ßÌåÏîÄ¿£¬½«ÊÇÒ»¸ö²»´íµÄÁ·Ï°¡£ÎªÁËʹÕâÒ»µã±äµÃ¸ü¼ÓÈÝÒ×£¬Àà¿ÉÒÔ´ÓÆäËüµÄÀàÖÐÀ©Õ¹³öÀ´¡£À©Õ¹»òÅÉÉú³öÀ´µÄÀàÓµÓÐÆä»ùÀࣨÕâ³ÆÎª¡°¼Ì³Ð¡±£¬Ö»²»¹ýûÈËËÀ£©µÄËùÓбäÁ¿ºÍº¯Êý£¬²¢°üº¬ËùÓÐÄãÔÚÅÉÉúÀàÖж¨ÒåµÄ²¿·Ö¡£ÀàÖеÄÔªËØ²»¿ÉÄܼõÉÙ£¬¾ÍÊÇ˵£¬²»¿ÉÒÔ×¢ÏúÈκδæÔڵĺ¯Êý»òÕß±äÁ¿¡£Ò»¸öÀ©³äÀà×ÜÊÇÒÀÀµÒ»¸öµ¥¶ÀµÄ»ùÀ࣬Ҳ¾ÍÊÇ˵£¬¶à¼Ì³ÐÊDz»Ö§³ÖµÄ¡£Ê¹Óùؼü×Ö¡°extends¡±À´À©Õ¹Ò»¸öÀà¡£ </para> <informalexample> @@ -211,34 +199,33 @@ </informalexample> <para> - ÉÏÊöʾÀý¶¨ÒåÁËÃûΪ Named_Cart µÄÀ࣬¸ÃÀàÓµÓÐ Cart ÀàµÄËùÓбäÁ¿ºÍº¯Êý£¬¼ÓÉÏ - ¸½¼ÓµÄ±äÁ¿ $owner ºÍÒ»¸ö¸½¼Óº¯Êý set_owner()¡£ÏÖÔÚ£¬ÄãÒÔÕý³£µÄ·½Ê½´´½¨ÁËÒ» - ¸ö named cart£¬²¢ÇÒ¿ÉÒÔÉèÖò¢È¡µÃ carts µÄËùÓÐÕß¡£¶øÕý³£µÄ cart ÀàµÄº¯ÊýÒÀ¾É - ¿ÉÒÔÔÚ named carts ÖÐʹÓ㺠+ ÉÏÊöʾÀý¶¨ÒåÁËÃûΪ Named_Cart µÄÀ࣬¸ÃÀàÓµÓÐ Cart + ÀàµÄËùÓбäÁ¿ºÍº¯Êý£¬¼ÓÉϸ½¼ÓµÄ±äÁ¿ $owner ºÍÒ»¸ö¸½¼Óº¯Êý + set_owner()¡£ÏÖÔÚ£¬ÄãÒÔÕý³£µÄ·½Ê½´´½¨ÁËÒ»¸öÓÐÃû×ֵĹºÎï³µ£¬²¢ÇÒ¿ÉÒÔÉèÖò¢È¡µÃ¸Ã¹ºÎï³µµÄÖ÷ÈË¡£¶øÕý³£µÄ¹ºÎï³µÀàµÄº¯ÊýÒÀ¾É¿ÉÒÔÔÚÓÐÃû×ֵĹºÎï³µÀàÖÐʹÓ㺠</para> <informalexample> <programlisting role="php"> <![CDATA[ -$ncart = new Named_Cart; // Create a named cart -$ncart->set_owner("kris"); // Name that cart -print $ncart->owner; // print the cart owners name -$ncart->add_item("10", 1); // (inherited functionality from cart) +$ncart = new Named_Cart; // н¨Ò»¸öÓÐÃû×ֵĹºÎï³µ +$ncart->set_owner("kris"); // ¸ø¸Ã¹ºÎï³µÃüÃû +print $ncart->owner; // Êä³ö¸Ã¹ºÎï³µÖ÷È˵ÄÃû×Ö +$ncart->add_item("10", 1); // £¨´Ó¹ºÎï³µÀàÖм̳ÐÀ´µÄ¹¦ÄÜ£© ]]> </programlisting> </informalexample> <para> - Õâ¸öÒ²¿ÉÒÔ½Ð×ö¡°¸¸£×Ó¡±¹ØÏµ¡£Äã´´½¨Ò»¸öÀ࣬¸¸À࣬²¢Ê¹Óà <literal>extends</literal> - À´´´½¨Ò»¸ö <emphasis>»ùÓÚ</emphasis> ¸¸ÀàµÄÐÂÀࣺ×ÓÀà¡£ÄãÉõÖÁ¿ÉÒÔʹÓÃÕâ¸öеÄ×ÓÀàÀ´ - ´´½¨ÁíÍâÒ»¸ö»ùÓÚÕâ¸ö×ÓÀàµÄÀà¡£ + Õâ¸öÒ²¿ÉÒÔ½Ð×ö¡°¸¸£×Ó¡±¹ØÏµ¡£Äã´´½¨Ò»¸öÀ࣬¸¸À࣬²¢Ê¹Óà <literal>extends</literal> + À´´´½¨Ò»¸ö<emphasis>»ùÓÚ</emphasis>¸¸ÀàµÄÐÂÀࣺ×ÓÀà¡£ÄãÉõÖÁ¿ÉÒÔʹÓÃÕâ¸öеÄ×ÓÀàÀ´´´½¨ÁíÍâÒ»¸ö»ùÓÚÕâ¸ö×ÓÀàµÄÀà¡£ </para> <note> <para> - ÀàÖ»ÓÐÔÚ¶¨Òåºó²Å¿ÉÒÔʹÓã¡Èç¹ûÄãÐëÒªÀà <literal>Named_Cart</literal> ¼Ì³ÐÀà <literal>Cart</literal>£¬ - Äã±ØÐëÊ×Ïȶ¨Òå <literal>Cart</literal> Àà¡£Èç¹ûÄãÐëÒª´´½¨ÁíÒ»¸ö»ùÓÚ <literal>Named_Cart</literal> Àà - µÄ <literal>Yellow_named_cart </literal> À࣬Äã±ØÐëÊ×Ïȶ¨Òå <literal>Named_Cart</literal> Àà¡£ - ¼ò½ÝµÄ˵£ºÀඨÒåµÄ˳ÐòÊǷdz£ÖØÒªµÄ¡£ + ÀàÖ»ÓÐÔÚ¶¨Òåºó²Å¿ÉÒÔʹÓã¡Èç¹ûÄãÐèÒªÀà <literal>Named_Cart</literal> + ¼Ì³ÐÀà <literal>Cart</literal>£¬Äã±ØÐëÊ×Ïȶ¨Òå <literal>Cart</literal> + Àà¡£Èç¹ûÄãÐëÒª´´½¨ÁíÒ»¸ö»ùÓÚ <literal>Named_Cart</literal> + ÀàµÄ <literal>Yellow_named_cart </literal> À࣬Äã±ØÐëÊ×Ïȶ¨Òå + <literal>Named_Cart</literal> Àà¡£¼ò½ÝµÄ˵£ºÀඨÒåµÄ˳ÐòÊǷdz£ÖØÒªµÄ¡£ </para> </note> </sect1> @@ -248,21 +235,21 @@ <caution> <simpara> - PHP 3 ºÍ PHP4 µÄ¹¹Ô캯ÊýÓÐËù²»Í¬¡£PHP 4 µÄÓïÒå¸ü¿ÉÈ¡¡£ + PHP 3 ºÍ PHP4 µÄ¹¹Ô캯ÊýÓÐËù²»Í¬¡£PHP 4 µÄÓïÒå¸ü¿ÉÈ¡¡£ </simpara> </caution> <para> - ¹¹Ô캯ÊýÊÇÀàÖеÄÒ»¸öÌØÊ⺯Êý£¬µ±Ê¹Óà <literal>new</literal> ²Ù×÷·û - ´´½¨Ò»¸öÀàµÄʵÀýʱ£¬¹¹Ô캯Êý½«»á×Ô¶¯µ÷Óá£PHP 3 ÖУ¬µ±º¯ÊýÓÚÀàͬÃûʱ£¬ - Õâ¸öº¯Êý½«³ÉΪ¹¹Ô캯Êý¡£PHP 4 £¬ÀàÖж¨ÒåµÄº¯ÊýÓëÀàͬÃûʱ£¬Õâ¸öº¯Êý½« - ³ÉΪһ¸ö¹¹Ô캯Êý£Çø±ðºÜ΢Ãµ«·Ç³£¹Ø¼ü£¨¼ûÏÂÎÄ£©¡£ + ¹¹Ô캯ÊýÊÇÀàÖеÄÒ»¸öÌØÊ⺯Êý£¬µ±Ê¹Óà <literal>new</literal> + ²Ù×÷·û´´½¨Ò»¸öÀàµÄʵÀýʱ£¬¹¹Ô캯Êý½«»á×Ô¶¯µ÷Óá£PHP 3 + ÖУ¬µ±º¯ÊýÓëÀàͬÃûʱ£¬Õâ¸öº¯Êý½«³ÉΪ¹¹Ô캯Êý¡£PHP 4 + ÖУ¬ÔÚÀàÀﶨÒåµÄº¯ÊýÓëÀàͬÃûʱ£¬Õâ¸öº¯Êý½«³ÉΪһ¸ö¹¹Ô캯Êý £ Çø±ðºÜ΢Ãµ«·Ç³£¹Ø¼ü£¨¼ûÏÂÎÄ£©¡£ </para> <informalexample> <programlisting role="php"> <![CDATA[ -// Works in PHP 3 and PHP 4. +// PHP 3 ºÍ PHP 4 Öж¼ÄÜÓà class Auto_Cart extends Cart { function Auto_Cart() @@ -275,16 +262,14 @@ </informalexample> <para> - ÉÏÎ͍ÒåÁËÒ»¸ö Auto_Cart À࣬¼´ Cart Àà¼ÓÉÏÒ»¸ö¹¹Ô캯Êý£¬µ±Ã¿´ÎʹÓà "new" ´´½¨ - Ò»¸öÐ嵀 Auto_Cart ÀàʵÀýʱ£¬¹¹Ô캯Êý½«×Ô¶¯µ÷Óò¢½«Ò»¼þÉÌÆ·µÄÊýÄ¿³õʼ»¯Îª ¡°10¡±¡£ - ¹¹Ô캯Êý¿ÉÒÔʹÓòÎÊý£¬¶øÇÒÕâЩ²ÎÊý¿ÉÑ¡µÄ£¬ËüÃÇ¿ÉÒÔʹ¹¹Ô캯Êý¸ü¼ÓÓÐÓá£ÎªÁËÒÀÈ»¿ÉÒÔ - ²»´ø²ÎÊýµÄʹÓÃÀ࣬ËùÓй¹Ô캯ÊýµÄ²ÎÊýÓ¦¸ÃÌṩĬÈÏÖµ£¬Ê¹Æä¿ÉÑ¡¡£ + ÉÏÎ͍ÒåÁËÒ»¸ö Auto_Cart À࣬¼´ Cart Àà¼ÓÉÏÒ»¸ö¹¹Ô캯Êý£¬µ±Ã¿´ÎʹÓá°new¡±´´½¨Ò»¸öÐ嵀 Auto_Cart + ÀàʵÀýʱ£¬¹¹Ô캯Êý½«×Ô¶¯µ÷Óò¢½«Ò»¼þÉÌÆ·µÄÊýÄ¿³õʼ»¯Îª¡°10¡±¡£¹¹Ô캯Êý¿ÉÒÔʹÓòÎÊý£¬¶øÇÒÕâЩ²ÎÊý¿ÉÒÔÊÇ¿ÉÑ¡µÄ£¬ËüÃÇ¿ÉÒÔʹ¹¹Ô캯Êý¸ü¼ÓÓÐÓá£ÎªÁËÒÀÈ»¿ÉÒÔ²»´ø²ÎÊýµØÊ¹ÓÃÀ࣬ËùÓй¹Ô캯ÊýµÄ²ÎÊýÓ¦¸ÃÌṩĬÈÏÖµ£¬Ê¹Æä¿ÉÑ¡¡£ </para> <informalexample> <programlisting role="php"> <![CDATA[ -// Works in PHP 3 and PHP 4. +// PHP 3 ºÍ PHP 4 Öж¼ÄÜÓà class Constructor_Cart extends Cart { function Constructor_Cart($item = "10", $num = 1) @@ -293,11 +278,11 @@ } } -// Shop the same old boring stuff. +// ÂòЩͬÑùÎÞÁĵÄÀÏ»õ $default_cart = new Constructor_Cart; -// Shop for real... +// ÂòЩж«Î÷... $different_cart = new Constructor_Cart("20", 17); ]]> @@ -305,14 +290,14 @@ </informalexample> <para> - ÄãÒ²¿ÉÒÔʹÓà <literal>@</literal> ²Ù×÷·ûÀ´ <emphasis>Ïû³ý</emphasis> ·¢ÉúÔÚ¹¹Ô캯ÊýÖеĴíÎó¡£e.g. - <literal>@new</literal>. + ÄãÒ²¿ÉÒÔʹÓà <literal>@</literal> + ²Ù×÷·ûÀ´<emphasis>Ïû³ý</emphasis>·¢ÉúÔÚ¹¹Ô캯ÊýÖеĴíÎó¡£ÀýÈç <literal>@new</literal>¡£ </para> <caution> <simpara> - PHP 3£¬ÅÉÉúÀàºÍ¹¹Ô캯ÊýÓÐÐí¶àÏÞÖÆ¡£×ÐϸÔĶÁÏÂÁз¶ÀýÒÔÀí½âÕâЩÏÞÖÆ¡£ + PHP 3 ÖÐÅÉÉúÀàºÍ¹¹Ô캯ÊýÓÐÐí¶àÏÞÖÆ¡£×ÐϸÔĶÁÏÂÁз¶ÀýÒÔÀí½âÕâЩÏÞÖÆ¡£ </simpara> </caution> @@ -335,20 +320,21 @@ } } -// no constructor is being called in PHP 3. +// PHP 3 ÖÐûÓй¹Ô캯Êý±»µ÷Óà $b = new B; ]]> </programlisting> </informalexample> <para> - PHP 3£¬ÔÚÉÏÃæµÄʾÀýÖн«²»»áÓй¹Ô캯Êý±»µ÷Óá£PHP 3 µÄ¹æÔòÊÇ£º¡°¹¹Ô캯ÊýÊÇÓëÀà - ͬÃûµÄº¯Êý¡±¡£ÕâÀÀàµÄÃû×ÖÊÇ B£¬µ«ÊÇÀà B ÖÐûÓк¯Êý B()¡£Ê²Ã´Ò²²»»á·¢Éú¡£ + PHP 3 ÖУ¬ÔÚÉÏÃæµÄʾÀýÖн«²»»áÓй¹Ô캯Êý±»µ÷Óá£PHP 3 + µÄ¹æÔòÊÇ£º¡°¹¹Ô캯ÊýÊÇÓëÀàͬÃûµÄº¯Êý¡±¡£ÕâÀÀàµÄÃû×ÖÊÇ B£¬µ«ÊÇÀà B + ÖÐûÓк¯Êý B()¡£Ê²Ã´Ò²²»»á·¢Éú¡£ </para> <para> - PHP 4 ÐÞÕýÁËÕâ¸öÎÊÌ⣬²¢½éÉÜÁËÁíÍâµÄйæÔò£ºÈç¹ûÒ»¸öÀàûÓй¹Ô캯Êý£¬Èç¹û¸¸ÀàÓй¹Ôì - º¯ÊýµÄ»°£¬¸¸ÀàµÄ¹¹Ô캯Êý½«»á±»µ÷Óá£PHP 4 ÖУ¬ÉÏÃæµÄÀý×Ó½«»áÊä³ö¡°I am the constructor of A.<br>¡±¡£ + PHP 4 ÐÞÕýÁËÕâ¸öÎÊÌ⣬²¢½éÉÜÁËÁíÍâµÄйæÔò£ºÈç¹ûÒ»¸öÀàûÓй¹Ô캯Êý£¬Èç¹û¸¸ÀàÓй¹Ô캯ÊýµÄ»°£¬¸¸ÀàµÄ¹¹Ô캯Êý½«»á±»µ÷Óá£PHP 4 + ÖУ¬ÉÏÃæµÄÀý×Ó½«»áÊä³ö¡°I am the constructor of A.<br>¡±¡£ </para> <informalexample> @@ -376,47 +362,49 @@ } } -// This will call B() as a constructor. +// µ÷Óà B() ×÷Ϊ¹¹Ô캯Êý $b = new B; ]]> </programlisting> </informalexample> <para> - Èç¹ûÊÇ PHP 3£¬A ÀàÖеĺ¯Êý B() ½«Á¢¼´³ÉΪÀà B ÖеĹ¹Ô캯Êý£¬ËäÈ»²¢²»ÊÇÓÐÒâÈç´Ë¡£ - PHP 3 ÖеĹæÔòÊÇ£º¡°¹¹Ô캯ÊýÊÇÓëÀàͬÃûµÄº¯Êý¡±¡£PHP 3 ²¢²»¹ØÐĺ¯ÊýÊDz»ÊÇÔÚ B Àà - Öж¨ÒåµÄ£¬»òÕßÊÇ·ñÒѾ±»¼Ì³Ð¡£ + Èç¹ûÊÇ PHP 3£¬Àà A Öеĺ¯Êý B() ½«Á¢¼´³ÉΪÀà B + ÖеĹ¹Ô캯Êý£¬ËäÈ»²¢²»ÊÇÓÐÒâÈç´Ë¡£PHP 3 + ÖеĹæÔòÊÇ£º¡°¹¹Ô캯ÊýÊÇÓëÀàͬÃûµÄº¯Êý¡±¡£PHP 3 + ²¢²»¹ØÐĺ¯ÊýÊDz»ÊÇÔÚÀà B + Öж¨ÒåµÄ£¬»òÕßÊÇ·ñÒѾ±»¼Ì³Ð¡£ </para> <para> - PHP 4 ÐÞ¸ÄÁ˹æÔò£º¡°¹¹Ô캯ÊýÓ붨ÒåÆä×ÔÉíµÄÀàͬÃû¡±¡£Òò¶øÔÚ PHP 4 ÖУ¬Àà B ½« - ²»»áÓÐÊôÓÚ×ÔÉíµÄ¹¹Ô캯Êý£¬²¢ÇÒ¸¸ÀàµÄ¹¹Ô캯Êý½«»á±»µ÷Ó㬴òÓ¡³ö - ¡°I am the constructor of A.<br>¡±¡£ - - [ÒëÕß×¢,by Verdana: ÕâÀïËÆºõÓÐÎÊÌ⣬ʵ¼ÊÊä³öµÄ½á¹û£¬²¢²»ÏóÕâÀï˵µÄÄÇÑù¡£Êµ¼ÊÊä³öµÄ - ÄÚÈÝÊÇ¡°I am a regular function named B in class A......¡±£¬Êä³öµÄÊÇ B() µÄÄÚÈÝ - Ò²¾ÍÊÇ˵£¬Àý×ÓÖеÄ×¢ÊÍ¡°This will call B() as a constructor¡±ÊÇÕýÈ·µÄ¡£Èç¹û´Ó A Àà - ÖÐÒÆ³ýº¯Êý B()£¬ÄÇô½«Êä³ö A() µÄÄÚÈÝ¡£] + PHP 4 ÐÞ¸ÄÁ˹æÔò£º¡°¹¹Ô캯ÊýÓ붨ÒåÆä×ÔÉíµÄÀàͬÃû¡±¡£Òò¶øÔÚ PHP 4 + ÖУ¬Àà B ½«²»»áÓÐÊôÓÚ×ÔÉíµÄ¹¹Ô캯Êý£¬²¢ÇÒ¸¸ÀàµÄ¹¹Ô캯Êý½«»á±»µ÷Óã¬Êä³ö¡°I + am the constructor of A.<br>¡±¡£ + </para> + <para> + ¡¾ÒëÕß×¢¡¿<!-- By Verdana -->ÕâÀïËÆºõÓÐÎÊÌ⣬ʵ¼ÊÊä³öµÄ½á¹û£¬²¢²»ÏóÕâÀï˵µÄÄÇÑù¡£Êµ¼ÊÊä³öµÄÄÚÈÝÊÇ¡°I + am a regular function named B in class A......¡±£¬Êä³öµÄÊÇ B() + µÄÄÚÈÝ¡£Ò²¾ÍÊÇ˵£¬Àý×ÓÖеÄ×¢ÊÍ¡°This will call B() as a constructor¡±ÊÇÕýÈ·µÄ¡£Èç¹û´Ó A + ÀàÖÐÒÆ³ýº¯Êý B()£¬ÄÇô½«Êä³ö A() µÄÄÚÈÝ¡£ </para> <caution> <simpara> - ²»¹ÜÊÇ PHP 3 »¹ÊÇ PHP 4 ¶¼²»ÄÜ´ÓÅÉÉúÀàµÄ¹¹Ô캯ÊýÖÐ×Ô¶¯µ÷ÓûùÀàµÄ¹¹Ô캯Êý¡£ - It is your reponsibility to prapagater the call to constructors - upstream where appropriate. + ²»¹ÜÊÇ PHP 3 »¹ÊÇ PHP 4 + ¶¼²»»á´ÓÅÉÉúÀàµÄ¹¹Ô캯ÊýÖÐ×Ô¶¯µ÷ÓûùÀàµÄ¹¹Ô캯Êý¡£Ç¡µ±µØÖð´Îµ÷ÓÃÉÏÒ»¼¶µÄ¹¹Ô캯ÊýÊÇÓû§µÄÔðÈΡ£ </simpara> </caution> <note> <simpara> - PHP 3 »òÕß PHP 4 Öж¼Ã»ÓÐÎö¹¹º¯Êý¡£Äã¿ÉÒÔʹÓà <function>register_shutdown_function</function> - º¯ÊýÀ´Ä£Äâ¶àÊýÎö¹¹º¯ÊýµÄЧ¹û¡£ + PHP 3 »òÕß PHP 4 Öж¼Ã»ÓÐÎö¹¹º¯Êý¡£Äã¿ÉÒÔʹÓà <function>register_shutdown_function</function> + º¯ÊýÀ´Ä£Äâ¶àÊýÎö¹¹º¯ÊýµÄЧ¹û¡£ </simpara> </note> <para> - µ±¶ÔÏó±»Ïú»Ù£¬Ê¹Óà <function>unset</function> »òÕß¼òµ¥µÄÍÑÀ뷶Χ£¬Îö¹¹º¯Êý¶¼»á×Ô¶¯µ÷Óᣠ- µ« PHP ÖÐûÓÐÎö¹¹º¯Êý¡£ + Îö¹¹º¯ÊýÊÇÒ»ÖÖµ±¶ÔÏó±»Ïú»Ùʱ£¬ÎÞÂÛʹÓÃÁË <function>unset</function> + »òÕß¼òµ¥µÄÍÑÀ뷶Χ£¬¶¼»á±»×Ô¶¯µ÷Óõĺ¯Êý¡£µ« PHP ÖÐûÓÐÎö¹¹º¯Êý¡£ </para> </sect1> @@ -430,8 +418,8 @@ </caution> <para> - ÓÐʱ£¬ÔÚûÓÐÉùÃ÷ÈκÎʵÀýµÄÇé¿öÏ·ÃÎÊÀàÖеĺ¯Êý»òÕß»ùÀàÖеĺ¯ÊýºÍ±äÁ¿ºÜÓÐÓô¦¡£ - ¶ø :: ²Ù×÷·û¼´ÓÃÓÚ´ËÇé¿ö¡£ + ÓÐʱ£¬ÔÚûÓÐÉùÃ÷ÈκÎʵÀýµÄÇé¿öÏ·ÃÎÊÀàÖеĺ¯Êý»òÕß»ùÀàÖеĺ¯ÊýºÍ±äÁ¿ºÜÓÐÓô¦¡£¶ø + :: ÔËËã·û¼´ÓÃÓÚ´ËÇé¿ö¡£ </para> <informalexample> @@ -454,15 +442,14 @@ } } -// there is no object of class A. -// this will print +// A ÀàûÓжÔÏó£¬Õ⽫Êä³ö // I am the original function A::example().<br> A::example(); -// create an object of class B. +// ½¨Á¢Ò»¸ö B ÀàµÄ¶ÔÏó $b = new B; -// this will print +// Õ⽫Êä³ö // I am the redefined function B::example().<br> // I am the original function A::example().<br> $b->example(); @@ -471,27 +458,26 @@ </informalexample> <para> - ÉÏÃæµÄÀý×Óµ÷ÓÃÁË A ÀàµÄº¯Êý example()£¬µ«ÊÇÕâÀï²¢²»´æÔÚ A ÀàµÄ¶ÔÏó£¬ - Òò´Ë²»ÄÜÕâÑùÓà $a->example() »òÕßÀàËÆµÄ·½·¨µ÷Óà example()¡£·´¶øÎÒÃÇ - ½« example() ×÷Ϊһ¸öÀຯÊýÀ´µ÷Óã¬Ò²¾ÍÊÇ˵£¬×÷Ϊһ¸öÀà×ÔÉíµÄº¯ÊýÀ´µ÷Óà - £¬¶ø²»ÊÇÕâ¸öÀàµÄÈκζÔÏó¡£ + ÉÏÃæµÄÀý×Óµ÷ÓÃÁË A ÀàµÄº¯Êý example()£¬µ«ÊÇÕâÀï²¢²»´æÔÚ A + ÀàµÄ¶ÔÏó£¬Òò´Ë²»ÄÜÕâÑùÓà $a->example() »òÕßÀàËÆµÄ·½·¨µ÷Óà + example()¡£·´¶øÎÒÃǽ« example() + ×÷Ϊһ¸öÀຯÊýÀ´µ÷Óã¬Ò²¾ÍÊÇ˵£¬×÷Ϊһ¸öÀà×ÔÉíµÄº¯ÊýÀ´µ÷Ó㬶ø²»ÊÇÕâ¸öÀàµÄÈκζÔÏó¡£ </para> <para> - ÕâÀïÓÐÀຯÊý£¬µ«Ã»ÓÐÀàµÄ±äÁ¿¡£Êµ¼ÊÉÏ£¬ÔÚµ÷Óú¯ÊýʱÍêȫûÓÐÈκζÔÏó¡£Òò¶ø - Ò»¸öÀàµÄº¯Êý¿ÉÒÔ²»Ê¹ÓÃÈκζÔÏ󣨵«¿ÉÒÔʹÓþֲ¿»òÕßÈ«¾Ö±äÁ¿£©£¬²¢ÇÒ¿ÉÒÔ¸ù±¾ - ²»Ê¹Óà $this ±äÁ¿¡£ + ÕâÀïÓÐÀຯÊý£¬µ«Ã»ÓÐÀàµÄ±äÁ¿¡£Êµ¼ÊÉÏ£¬ÔÚµ÷Óú¯ÊýʱÍêȫûÓÐÈκζÔÏó¡£Òò¶øÒ»¸öÀàµÄº¯Êý¿ÉÒÔ²»Ê¹ÓÃÈκζÔÏ󣨵«¿ÉÒÔʹÓþֲ¿»òÕßÈ«¾Ö±äÁ¿£©£¬²¢ÇÒ¿ÉÒÔ¸ù±¾²»Ê¹Óà $this ±äÁ¿¡£ </para> <para> - ÉÏÃæµÄÀý×ÓÖУ¬Àà B ÖØÐ¶¨ÒåÁ˺¯Êý example()¡£A ÀàÖÐÔʼ¶¨ÒåµÄº¯Êý example() - ½«±»ÆÁ±Î²¢ÇÒ²»ÔÙÉúЧ£¬³ý·ÇÄãʹÓà :: ²Ù×÷·ûÀ´·ÃÎÊ A ÀàÖÐµÄ example() º¯Êý¡£ - È磺 A::example() £¨Êµ¼ÊÉÏ£¬Äã±ØÐë±àд parent::example()£¬ÏÂÒ»Õ½éÉܸÃÄÚÈÝ¡££© + ÉÏÃæµÄÀý×ÓÖУ¬Àà B ÖØÐ¶¨ÒåÁ˺¯Êý example()¡£A + ÀàÖÐÔʼ¶¨ÒåµÄº¯Êý example() + ½«±»ÆÁ±Î²¢ÇÒ²»ÔÙÉúЧ£¬³ý·ÇÄãʹÓà :: + ÔËËã·ûÀ´·ÃÎÊ A ÀàÖÐµÄ example() º¯Êý¡£È磺A::example()£¨Êµ¼ÊÉÏ£¬Ó¦¸ÃдΪ + parent::example()£¬ÏÂÒ»Õ½éÉܸÃÄÚÈÝ¡££© </para> <para> - ¾Í´Ë¶øÂÛ£¬¶ÔÓÚµ±Ç°¶ÔÏó£¬Ëü¿ÉÄÜÓжÔÏó±äÁ¿¡£Òò¶ø£¬Äã¿ÉÒÔÔÚ¶ÔÏóº¯ÊýµÄÄÚ²¿ - ʹÓà $this ºÍ¶ÔÏó±äÁ¿¡£ + ¾Í´Ë¶øÂÛ£¬¶ÔÓÚµ±Ç°¶ÔÏó£¬Ëü¿ÉÄÜÓжÔÏó±äÁ¿¡£Òò¶ø£¬Äã¿ÉÒÔÔÚ¶ÔÏóº¯ÊýµÄÄÚ²¿Ê¹Óà $this ºÍ¶ÔÏó±äÁ¿¡£ </para> </sect1> @@ -500,23 +486,15 @@ <title><literal>parent</literal></title> <para> - µ±Äã·¢ÏÖ×Ô¼ºÕýÔÚдһЩ·ÃÎÊ»ùÀàµÄ±äÁ¿ºÍº¯ÊýµÄ´úÂëʱ£¬Õâ³ä·Ö˵Ã÷ÁË - ÄãµÄÅÉÉúÀà·Ç³£µÄ¾«Á·¡¢×¨Òµ¡£ + Äã¿ÉÄܻᷢÏÖ×Ô¼ºÐ´µÄ´úÂë·ÃÎÊÁË»ùÀàµÄ±äÁ¿ºÍº¯Êý¡£Èç¹ûÅÉÉúÀà·Ç³£¾«Á¶»òÕß»ùÀà·Ç³£×¨Òµ»¯µÄʱºòÓÈÆäÊÇÕâÑù¡£ </para> <para> - Instead of using the literal name of the base class in your - code, you should be using the special name - <literal>parent</literal>, which refers to the name of your - base class as given in the <literal>extends</literal> - declaration of your class. By doing this, you avoid using the - name of your base class in more than one place. Should - your inheritance tree change during implementation, the - change is easily made by simply changing the - <literal>extends</literal> declaration of your class. - - ÄãÓ¦¸ÃÔÚ´úÂëÖÐʹÓÃרÓÃÃû³Æ <literal>parent</literal> Ìæ´ú»ùÀàµÄÃû³Æ£¬ - À´·ÃÎÊ + ²»ÒªÓôúÂëÖлùÀàÎÄ×ÖÉϵÄÃû×Ö£¬Ó¦¸ÃÓÃÌØÊâµÄÃû×Ö + <literal>parent</literal>£¬ËüÖ¸µÄ¾ÍÊÇÅÉÉúÀàÔÚ + <literal>extends</literal> ÉùÃ÷ÖÐËùÖ¸µÄ»ùÀàµÄÃû×Ö¡£ÕâÑù×ö¿ÉÒÔ±ÜÃâÔÚ¶à¸öµØ·½Ê¹ÓûùÀàµÄÃû×Ö¡£ + Èç¹û¼Ì³ÐÊ÷ÔÚʵÏֵĹý³ÌÖÐÒªÐ޸ģ¬Ö»Òª¼òµ¥µØÐÞ¸ÄÀàÖÐ + <literal>extends</literal> ÉùÃ÷µÄ²¿·Ö¡£ </para> <informalexample> @@ -541,7 +519,7 @@ $b = new B; -// This will call B::example(), which will in turn call A::example(). +// Õ⽫µ÷Óà B::example()£¬¶øËü»áÈ¥µ÷Óà A::example()¡£ $b->example(); ]]> </programlisting> @@ -549,45 +527,34 @@ </sect1> <sect1 id="language.oop.serialization"> - <title>ÐòÁл¯¶ÔÏó£»á»°ÖеĶÔÏó</title> + <title>ÐòÁл¯¶ÔÏó £ »á»°ÖеĶÔÏó</title> <note> <simpara> - In PHP 3, objects will lose their class association - throughout the process of serialization and unserialization. - The resulting variable is of type object, but has no class - and no methods, thus it is pretty useless (it has become - just like an array with a funny syntax). + ÔÚ PHP 3 ÖУ¬ÔÚÐòÁл¯ºÍ½âÐòÁл¯µÄ¹ý³ÌÖжÔÏó»áʧȥÀàµÄ¹ØÁª¡£½á¹ûµÄ±äÁ¿ÊǶÔÏóÀàÐÍ£¬µ«ÊÇûÓÐÀàºÍ·½·¨£¬Òò´Ë¾ÍûʲôÓÃÁË£¨¾ÍºÃÏñÒ»¸öÓû¬»üµÄÓï·¨¶¨ÒåµÄÊý×éÒ»Ñù£©¡£ </simpara> </note> <caution> <simpara> - The following information is valid for PHP 4 only. + ÒÔÏÂÐÅÏ¢½öÔÚ PHP 4 ÖÐÓÐЧ¡£ </simpara> </caution> <para> - <function>serialize</function> returns a string containing a - byte-stream representation of any value that can be stored in - PHP. <function>unserialize</function> can use this string to - recreate the original variable values. Using serialize to - save an object will save all variables in an object. The - functions in an object will not be saved, only the name of - the class. + <function>serialize</function> + ·µ»ØÒ»¸ö×Ö·û´®£¬°üº¬×Å¿ÉÒÔ´¢´æÓÚ PHP µÄÈκÎÖµµÄ×Ö½ÚÁ÷±íʾ¡£<function>unserialize</function> + ¿ÉÒÔÓôË×Ö·û´®À´Öؽ¨ÔʼµÄ±äÁ¿Öµ¡£ÓÃÐòÁл¯À´±£´æ¶ÔÏó¿ÉÒÔ±£´æ¶ÔÏóÖеÄËùÓбäÁ¿¡£¶ÔÏóÖеĺ¯Êý²»»á±»±£´æ£¬Ö»ÓÐÀàµÄÃû³Æ¡£ </para> <para> - In order to be able to <function>unserialize</function> an - object, the class of that object needs to be defined. That - is, if you have an object $a of class A on page1.php and - serialize this, you'll get a string that refers to class A - and contains all values of variabled contained in $a. If - you want to be able to unserialize this on page2.php, - recreating $a of class A, the definition of class A must - be present in page2.php. This can be done for example - by storing the class defintion of class A in an include - file and including this file in both page1.php and page2.php. + ÒªÄܹ» <function>unserialize</function> + Ò»¸ö¶ÔÏó£¬ÐèÒª¶¨Òå¸Ã¶ÔÏóµÄÀà¡£Ò²¾ÍÊÇ£¬Èç¹ûÐòÁл¯ÁË + page1.php ÖÐÀà A µÄ¶ÔÏó $a£¬½«µÃµ½Ò»¸öÖ¸ÏòÀà A + µÄ×Ö·û´®²¢°üº¬ÓÐËùÓÐ $a ÖбäÁ¿µÄÖµ¡£Èç¹ûÒªÔÚ + page2.php Öн«Æä½âÐòÁл¯£¬Öؽ¨Àà A µÄ¶ÔÏó + $a£¬Ôò page2.php ÖбØÐëÒª³öÏÖÀà A µÄ¶¨Òå¡£Õâ¿ÉÒÔÀýÈçÕâÑùʵÏÖ£¬½«Àà + A µÄ¶¨Òå·ÅÔÚÒ»¸ö°üº¬ÎļþÖУ¬²¢ÔÚ page1.php ºÍ page2.php ¶¼°üº¬´ËÎļþ¡£ </para> <informalexample> @@ -609,91 +576,77 @@ $a = new A; $s = serialize($a); - // store $s somewhere where page2.php can find it. + // ½« $s ´æ·ÅÔÚij´¦Ê¹ page2.php Äܹ»ÕÒµ½ $fp = fopen("store", "w"); fputs($fp, $s); fclose($fp); page2.php: - // this is needed for the unserialize to work properly. + // ΪÁËÕý³£½âÐòÁл¯ÐèÒªÕâÒ»ÐÐ include("classa.inc"); $s = implode("", @file("store")); $a = unserialize($s); - // now use the function show_one() of the $a object. + // ÏÖÔÚ¿ÉÒÔÓà $a ¶ÔÏóµÄ show_one() º¯ÊýÁË $a->show_one(); ]]> </programlisting> </informalexample> <para> - If you are using sessions and use <function>session_register</function> - to register objects, these objects are serialized automatically - at the end of each PHP page, and are unserialized automatically on - each of the following pages. This basically means that these objects - can show up on any of your pages once they become part of your - session. + Èç¹ûÔÚÓûỰ²¢Ê¹ÓÃÁË + <function>session_register</function> + À´×¢²á¶ÔÏó£¬ÕâЩ¶ÔÏó»áÔÚÿ¸ö PHP + Ò³Ãæ½áÊøÊ±±»×Ô¶¯ÐòÁл¯£¬²¢ÔÚ½ÓÏÂÀ´µÄÿ¸öÒ³ÃæÖÐ×Ô¶¯½âÐòÁл¯¡£»ù±¾ÉÏÊÇ˵ÕâЩ¶ÔÏóÒ»µ©³ÉΪ»á»°µÄÒ»²¿·Ö£¬¾ÍÄÜÔÚÈκÎÒ³ÃæÖгöÏÖ¡£ </para> <para> - It is strongly recommended that you include the class - definitions of all such registered objects on all of your - pages, even if you do not actually use these classes on all - of your pages. If you don't and an object is being - unserialized without its class definition being present, it - will lose its class association and become an object of class - <literal>stdClass</literal> without any functions available - at all, that is, it will become quite useless. + Ç¿ÁÒ½¨ÒéÔÚËùÓеÄÒ³ÃæÖж¼°üÀ¨ÕâЩע²áµÄ¶ÔÏóµÄÀàµÄ¶¨Ò壬¼´Ê¹²¢²»ÊÇÔÚËùÓеÄÒ³ÃæÖж¼Óõ½ÁËÕâЩÀà¡£Èç¹ûûÓÐÕâÑù×ö£¬Ò»¸ö¶ÔÏó±»½âÐòÁл¯Á˵«È´Ã»ÓÐÆäÀàµÄ¶¨Ò壬Ëü½«Ê§È¥ÓëÖ®¹ØÁªµÄÀಢ³ÉΪ + <literal>stdClass</literal> + µÄÒ»¸ö¶ÔÏó¶øÍêȫûÓÐÈκοÉÓõĺ¯Êý£¬ÕâÑù¾ÍºÜûÓÐÓô¦¡£ </para> <para> - So if in the example above $a became part of a session by - running <literal>session_register("a")</literal>, you should - include the file <literal>classa.inc</literal> on all of your - pages, not only page1.php and page2.php. + Òò´ËÈç¹ûÔÚÒÔÉϵÄÀý×ÓÖÐ $a ͨ¹ýÔËÐÐ + <literal>session_register("a")</literal> + ³ÉΪÁ˻ỰµÄÒ»²¿·Ö£¬Ó¦¸ÃÔÚËùÓеÄÒ³ÃæÖаüº¬ + <literal>classa.inc</literal> Îļþ£¬¶ø²»Ö»ÊÇ + page1.php ºÍ page2.php¡£ </para> </sect1> <sect1 id="language.oop.magic-functions"> - <title>The magic functions <literal>__sleep</literal> and <literal>__wakeup</literal></title> + <title>ħÊõº¯Êý <literal>__sleep</literal> ºÍ <literal>__wakeup</literal></title> <para> - <function>serialize</function> checks if your class has a function with - the magic name <literal>__sleep</literal>. If so, that function is - being run prior to any serialization. It can clean up the object - and is supposed to return an array with the names of all variables - of that object that should be serialized. + <function>serialize</function> ¼ì²éÀàÖÐÊÇ·ñÓÐħÊõÃû³Æ + <literal>__sleep</literal> + µÄº¯Êý¡£Èç¹ûÕâÑù£¬¸Ãº¯Êý½«ÔÚÈκÎÐòÁл¯Ö®Ç°ÔËÐС£Ëü¿ÉÒÔÇå³ý¶ÔÏó²¢Ó¦¸Ã·µ»ØÒ»¸ö°üº¬ÓиöÔÏóÖÐÓ¦±»ÐòÁл¯µÄËùÓбäÁ¿ÃûµÄÊý×é¡£ </para> <para> - The intended use of <literal>__sleep</literal> is to close any - database connections that object may have, committing pending - data or perform similar cleanup tasks. Also, the function is - useful if you have very large objects which need not be - saved completely. + ʹÓà <literal>__sleep</literal> + µÄÄ¿µÄÊǹرնÔÏó¿ÉÄܾßÓеÄÈκÎÊý¾Ý¿âÁ¬½Ó£¬Ìá½»µÈ´ýÖеÄÊý¾Ý»ò½øÐÐÀàËÆµÄÇå³ýÈÎÎñ¡£´ËÍ⣬Èç¹ûÓзdz£´óµÄ¶ÔÏó¶ø²¢²»ÐèÒªÍêÈ«´¢´æÏÂÀ´Ê±´Ëº¯ÊýÒ²ºÜÓÐÓᣠ</para> <para> - Conversely, <function>unserialize</function> checks for the - presence of a function with the magic name - <literal>__wakeup</literal>. If present, this function can - reconstruct any resources that object may have. + Ïà·´µØ£¬<function>unserialize</function> + ¼ì²é¾ßÓÐħÊõÃû³Æ + <literal>__wakeup</literal> + µÄº¯ÊýµÄ´æÔÚ¡£Èç¹û´æÔÚ£¬´Ëº¯Êý¿ÉÒÔÖØ½¨¶ÔÏó¿ÉÄܾßÓеÄÈκÎ×ÊÔ´¡£ </para> <para> - The intended use of <literal>__wakeup</literal> is to - reestablish any database connections that may have been lost - during serialization and perform other reinitialization - tasks. + ʹÓà <literal>__wakeup</literal> + µÄÄ¿µÄÊÇÖØ½¨ÔÚÐòÁл¯ÖпÉÄܶªÊ§µÄÈκÎÊý¾Ý¿âÁ¬½ÓÒÔ¼°´¦ÀíÆäËüÖØÐ³õʼ»¯µÄÈÎÎñ¡£ </para> </sect1> <sect1 id="language.oop.newref"> - <title>References inside the constructor</title> + <title>¹¹Ô캯ÊýÖеÄÒýÓÃ</title> <para> - Creating references within the constructor can lead to confusing - results. This tutorial-like section helps you to avoid problems. + ÔÚ¹¹Ô캯ÊýÖд´½¨ÒýÓÿÉÄܻᵼÖ»ìÏýµÄ½á¹û¡£±¾½ÚÒԽ̳ÌÐÎʽ°ïÖú±ÜÃâÎÊÌâ¡£ <informalexample> <programlisting role="php"> @@ -702,12 +655,12 @@ { function Foo($name) { - // create a reference inside the global array $globalref + // ÔÚÈ«¾ÖÊý×é $globalref Öн¨Á¢Ò»¸öÒýÓà global $globalref; $globalref[] = &$this; - // set name to passed value + // ½«Ãû×ÖÉ趨Ϊ´«µÝµÄÖµ $this->setName($name); - // and put it out + // ²¢Êä³öÖ® $this->echoName(); } @@ -727,11 +680,15 @@ </para> <para> - Let us check out if there is a difference between - <varname>$bar1</varname> which has been created using - the copy <literal>=</literal> operator and - <varname>$bar2</varname> which has been created using - the reference <literal>=&</literal> operator... + ÏÂÃæÀ´¼ì²éÒ»ÏÂÓÿ½±´ÔËËã·û + <literal>=</literal> + ´´½¨µÄ + <varname>$bar1</varname> + ºÍÓÃÒýÓÃÔËËã·û + <literal>=&</literal> + ´´½¨µÄ + <varname>$bar2</varname> + ÓÐûÓÐÇø±ð... <informalexample> <programlisting role="php"> @@ -740,7 +697,7 @@ $bar1->echoName(); $globalref[0]->echoName(); -/* output: +/* Êä³ö£º set in constructor set in constructor set in constructor */ @@ -749,7 +706,7 @@ $bar2->echoName(); $globalref[1]->echoName(); -/* output: +/* Êä³ö£º set in constructor set in constructor set in constructor */ @@ -758,49 +715,41 @@ </informalexample> </para> <para> - Apparently there is no difference, but in fact there is a - very significant one: <varname>$bar1</varname> and - <varname>$globalref[0]</varname> are _NOT_ referenced, they - are NOT the same variable. This is because "new" does not - return a reference by default, instead it returns a copy. + ÏÔȻûÓÐÇø±ð£¬µ«Êµ¼ÊÉÏÓÐÒ»¸ö·Ç³£ÖØÒªµÄÇø±ð£º<varname>$bar1</varname> + ºÍ <varname>$globalref[0]</varname> + ²¢Ã»Óб»ÒýÓã¬ËüÃDz»ÊÇͬһ¸ö±äÁ¿¡£ÕâÊÇÒòΪ¡°new¡±Ä¬Èϲ¢²»·µ»ØÒýÓ㬶ø·µ»ØÒ»¸ö¿½±´¡£ <note> <simpara> - There is no performance loss (since PHP 4 and up use reference - counting) returning copies instead of references. On the - contrary it is most often better to simply work with copies - instead of references, because creating references takes some - time where creating copies virtually takes no time (unless none - of them is a large array or object and one of them gets changed - and the other(s) one(s) subsequently, then it would be wise to - use references to change them all concurrently). + ÔÚ·µ»Ø¿½±´¶ø²»ÊÇÒýÓÃÖв¢Ã»ÓÐÐÔÄÜÉϵÄËðʧ£¨ÒòΪ + PHP 4 ¼°ÒÔÉϰ汾ʹÓÃÁËÒýÓüÆÊý£©¡£Ïà·´¸ü¶àÇé¿öϹ¤×÷ÓÚ¿½±´¶ø²»ÊÇÒýÓÃÉϸüºÃ£¬ÒòΪ½¨Á¢ÒýÓÃÐèҪһЩʱ¼ä¶ø½¨Á¢¿½±´Êµ¼ÊÉϲ»»¨Ê±¼ä£¨³ý·ÇËüÃǶ¼²»ÊÇ´óµÄÊý×é»ò¶ÔÏ󣬶øÆäÖÐÖ®Ò»¸ú×ÅÁíÒ»¸ö±ä£¬ÄÇʹÓÃÒýÓÃÀ´Í¬Ê±ÐÞ¸ÄËüÃÇ»á¸ü´ÏÃ÷һЩ£©¡£ </simpara> </note> - To prove what is written above let us watch the code below. + ÒªÖ¤Ã÷ÒÔÉÏдµÄ£¬¿´¿´ÏÂÃæµÄ´úÂë¡£ <informalexample> <programlisting role="php"> <![CDATA[ -// now we will change the name. what do you expect? -// you could expect that both $bar1 and $globalref[0] change their names... +// ÏÖÔڸĸöÃû×Ö£¬ÄãÔ¤ÆÚʲô½á¹û£¿ +// Äã¿ÉÄÜÔ¤ÆÚ $bar1 ºÍ $globalref[0] ¶þÕßµÄÃû×Ö¶¼¸ÄÁË... $bar1->setName('set from outside'); -// as mentioned before this is not the case. +// µ«ÈçÍ¬Ç°ÃæËµµÄ£¬²¢²»ÊÇÕâÑù¡£ $bar1->echoName(); $globalref[0]->echoName(); -/* output: +/* Êä³öΪ£º set from outside set in constructor */ -// let us see what is different with $bar2 and $globalref[1] +// ÏÖÔÚ¿´¿´ $bar2 ºÍ $globalref[1] ÓÐûÓÐÇø±ð $bar2->setName('set from outside'); -// luckily they are not only equal, they are the same variable -// thus $bar2->name and $globalref[1]->name are the same too +// ÐÒÔ˵ÄÊÇËüÃDz»µ«Ïàͬ£¬¸ù±¾¾ÍÊÇͬһ¸ö±äÁ¿¡£ +// Òò´Ë $bar2->name ºÍ $globalref[1]->name Ò²ÊÇͬһ¸ö±äÁ¿¡£ $bar2->echoName(); $globalref[1]->echoName(); -/* output: +/* Êä³öΪ£º set from outside set from outside */ ]]> @@ -808,7 +757,7 @@ </informalexample> </para> <para> - Another final example, try to understand it. + ×îºó¸ø³öÁíÒ»¸öÀý×Ó£¬ÊÔ×ÅÀí½âËü¡£ <informalexample> <programlisting role="php"> @@ -818,7 +767,7 @@ function A($i) { $this->value = $i; - // try to figure out why we do not need a reference here + // ÊÔ×ÅÏëÃ÷°×ΪʲôÕâÀï²»ÐèÒªÒýÓà $this->b = new B($this); } @@ -847,8 +796,8 @@ } } -// try to undestand why using a simple copy here would yield -// in an undesired result in the *-marked line +// ÊÔ×ÅÀí½âΪʲôÕâÀïÒ»¸ö¼òµ¥µÄ¿½±´»áÔÚÏÂÃæÓà * +// ±ê³öÀ´µÄÐÐÖвúÉúÔ¤ÆÚÖ®ÍâµÄ½á¹û $a =& new A(10); $a->createRef(); @@ -863,7 +812,7 @@ $a->c->echoValue(); /* -output: +Êä³öΪ£º class A: 10 class B: 10 class B: 10
Index: phpdoc-zh/translation.xml diff -u phpdoc-zh/translation.xml:1.92 phpdoc-zh/translation.xml:1.93 --- phpdoc-zh/translation.xml:1.92 Thu Mar 6 09:19:57 2003 +++ phpdoc-zh/translation.xml Sat Mar 8 04:19:57 2003 @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="gb2312" ?> <!DOCTYPE translation SYSTEM "../dtds/translation.dtd"> -<!-- $Id: translation.xml,v 1.92 2003/03/06 14:19:57 dallas Exp $ --> +<!-- $Id: translation.xml,v 1.93 2003/03/08 09:19:57 dallas Exp $ --> <translation> <intro> @@ -36,8 +36,6 @@ ×¢:·ÒëÍê³ÉµÄÎļþÇë´ÓÁбíÖÐɾ³ý. --> - <file name="language/oop.xml" person="verdana" type="working" revision="1.36" date="15/08/2002" /> - <file name="chapters/security.xml" person="Gregory" type="working" revision="1.49" date="25/12/2002" /> <file name="appendices/phpdevel.xml" person="class007" type="working" /> Index: phpdoc-zh/language/oop.xml diff -u phpdoc-zh/language/oop.xml:1.6 phpdoc-zh/language/oop.xml:1.7 --- phpdoc-zh/language/oop.xml:1.6 Mon Sep 16 00:50:58 2002 +++ phpdoc-zh/language/oop.xml Sat Mar 8 04:19:57 2003 @@ -1,7 +1,8 @@ -<?xml version="1.0" encoding="iso-8859-1"?> -<!-- $Revision: 1.6 $ --> -<!-- $Author: gregory $--> -<!-- EN-Revision: 1.37 Maintainer: verdana Status:partly --> +<?xml version="1.0" encoding="gb2312"?> +<!-- $Revision: 1.7 $ --> +<!-- $Author: dallas $--> +<!-- EN-Revision: 1.37 Maintainer: verdana Status: ready --> +<!-- CREDITS: dallas --> <chapter id="language.oop"> <title>ÀàÓë¶ÔÏó</title> @@ -15,16 +16,16 @@ <?php class Cart { - var $items; // Items in our shopping cart + var $items; // ¹ºÎï³µÖеÄÏîÄ¿ - // Add $num articles of $artnr to the cart + // °Ñ $num ¸ö $artnr ·ÅÈë³µÖÐ function add_item ($artnr, $num) { $this->items[$artnr] += $num; } - // Take $num articles of $artnr out of the cart + // °Ñ $num ¸ö $artnr ´Ó³µÖÐÈ¡³ö function remove_item ($artnr, $num) { @@ -43,46 +44,42 @@ </para> <para> - ÉÏÃæµÄÀý×Ó¶¨ÒåÁËÒ»¸ö Cart À࣬Õâ¸öÀàÓɹºÎï³µÖеÄÉÌÆ·¹¹³ÉµÄÊý×éºÍÁ½¸öÓÃÓÚ - ´Ó¹ºÎï³µÖÐÌí¼ÓºÍɾ³ýÉÌÆ·µÄº¯Êý×é³É¡£ + ÉÏÃæµÄÀý×Ó¶¨ÒåÁËÒ»¸ö Cart + À࣬Õâ¸öÀàÓɹºÎï³µÖеÄÉÌÆ·¹¹³ÉµÄÊý×éºÍÁ½¸öÓÃÓÚ´Ó¹ºÎï³µÖÐÌí¼ÓºÍɾ³ýÉÌÆ·µÄº¯Êý×é³É¡£ </para> <caution> <simpara> - ÏÂÃæµÄ¾¯¸æ×¢½â¶Ô PHP4 ÓÐЧ¡£ + ÏÂÃæµÄ¾¯¸æ×¢½â¶Ô PHP4 ÓÐЧ¡£ </simpara> <simpara> - Ãû³Æ <literal>stdClass</literal> ÒѾ±» Zend ʹÓò¢±£Áô¡£Äú²»ÄÜÔÚÄúµÄ PHP ´úÂë - Öж¨ÒåÃûΪ <literal>stdClass</literal> µÄÀà¡£ + Ãû³Æ <literal>stdClass</literal> ÒѾ±» Zend ʹÓò¢±£Áô¡£Äú²»ÄÜÔÚÄúµÄ PHP + ´úÂëÖж¨ÒåÃûΪ <literal>stdClass</literal> µÄÀà¡£ </simpara> <simpara> - The function names <literal>__sleep</literal> and - <literal>__wakeup</literal> are magical in PHP classes. You - cannot have functions with these names in any of your - classes unless you want the magic functionality associated - with them. See below for more information. + º¯ÊýÃû <literal>__sleep</literal> ºÍ + <literal>__wakeup</literal> ÔÚ + PHP ÀàÖÐÊÇħÊõº¯Êý¡£³ý·ÇÏëÒªÓëÖ®ÁªÏµµÄħÊõ¹¦ÄÜ£¬·ñÔòÔÚÈκÎÀàÖж¼²»ÄÜÒÔ´ËÃüÃûº¯Êý¡£ </simpara> <simpara> - PHP reserves all function names starting with __ as magical. - It is recommended that you do not use function names with - __ in PHP unless you want some documented magic functionality. + PHP ½«ËùÓÐÒÔ __ ¿ªÍ·µÄº¯ÊýÃû±£ÁôΪħÊõº¯Êý¡£³ý·ÇÏëҪʹÓÃһЩ¼ûÓÚÎĵµÖеÄħÊõ¹¦ÄÜ£¬·ñÔò½¨Òé²»ÒªÔÚ + PHP Öн«º¯ÊýÃûÒÔ __ ¿ªÍ·¡£ </simpara> </caution> <note> <simpara> - ÔÚ PHP 4 ÖУ¬<literal>var</literal> ±äÁ¿µÄÖµÖ»Äܳõʼ»¯Îª³£Á¿¡£Ó÷dz£Á¿Öµ³õʼ»¯±äÁ¿£¬ - ÄúÐèÒªÒ»¸ö³õʼ»¯º¯Êý£¬¸Ãº¯ÊýÔÚ¶ÔÏó±»´´½¨Ê±×Ô¶¯±»µ÷Óá£ÕâÑùÒ»¸öº¯Êý±»³ÆÖ®Îª¹¹Ô캯Êý - £¨¼ûÏÂÃæ£©¡£ + ÔÚ PHP 4 ÖУ¬<literal>var</literal> + ±äÁ¿µÄÖµÖ»Äܳõʼ»¯Îª³£Á¿¡£Ó÷dz£Á¿Öµ³õʼ»¯±äÁ¿£¬ÄúÐèÒªÒ»¸ö³õʼ»¯º¯Êý£¬¸Ãº¯ÊýÔÚ¶ÔÏó±»´´½¨Ê±×Ô¶¯±»µ÷Óá£ÕâÑùÒ»¸öº¯Êý±»³ÆÖ®Îª¹¹Ô캯Êý£¨¼ûÏÂÃæ£©¡£ </simpara> <informalexample> <programlisting role="php"> <![CDATA[ <?php -/* None of these will work in PHP 4. */ +/* PHP 4 Öв»ÄÜÕâÑùÓà */ class Cart { var $todays_date = date("Y-m-d"); @@ -91,7 +88,7 @@ var $items = array("VCR", "TV"); } -/* This is how it should be done. */ +/* Ó¦¸ÃÕâÑù½øÐÐ */ class Cart { var $todays_date; @@ -113,9 +110,8 @@ </note> <para> - Classes are types, that is, they are blueprints for actual - variables. You have to create a variable of the desired type with - the <literal>new</literal> operator. + ÀàÒ²ÊÇÒ»ÖÖÀàÐÍ£¬¾ÍÊÇ˵£¬ËüÃÇÊÇʵ¼Ê±äÁ¿µÄÀ¶Í¼¡£±ØÐëÓà + <literal>new</literal> ÔËËã·ûÀ´´´½¨ÏàÓ¦ÀàÐ͵ıäÁ¿¡£ </para> <informalexample> @@ -133,31 +129,30 @@ <para> ÉÏÊö´úÂë´´½¨ÁËÁ½¸ö Cart ÀàµÄ¶ÔÏó $cart ºÍ $another_cart, ¶ÔÏó $cart µÄ·½·¨ add_item() - ±»µ÷ÓÃʱ£¬Ìí¼ÓÁË1¼þ10ºÅÉÌÆ·¡£¶ÔÓÚ¶ÔÏó $another_cart, 3¼þ0815ºÅÉÌÆ·±»Ìí¼Óµ½¹ºÎï³µÖС£ + ±»µ÷ÓÃʱ£¬Ìí¼ÓÁË 1 ¼þ 10 ºÅÉÌÆ·¡£¶ÔÓÚ¶ÔÏó $another_cart, 3 ¼þ 0815 ºÅÉÌÆ·±»Ìí¼Óµ½¹ºÎï³µÖС£ </para> <para> - $cart ºÍ $another_cart ¶¼Óз½·¨ add_item(), remove_item() ºÍÒ»¸ö items ±äÁ¿¡£ - ËüÃǶ¼ÊÇÃ÷ÏԵĺ¯ÊýºÍ±äÁ¿¡£Äã¿ÉÒÔ°ÑËüÃǵ±×÷ÎļþϵͳÖеÄijЩÀàËÆÄ¿Â¼µÄ¶«Î÷À´¿¼ÂÇ¡£ - ÔÚÎļþϵͳÖУ¬Äã¿ÉÒÔÓµÓÐÁ½¸ö²»Í¬µÄ README.TXT Îļþ£¬Ö»Òª²»ÔÚÏàͬµÄĿ¼ÖС£ - ÕýÈç´ÓΪÁ˸ùĿ¼·ÃÎÊÿ¸öÎļþÄãÐèÒªÊäÈë¸ÃÎļþµÄÍêÕûµÄ·¾¶ÃûÒ»Ñù£¬Äã±ØÐëÖ¸¶¨Ðè - Òªµ÷Óõĺ¯ÊýµÄÍêÕûÃû³Æ£ºÔÚ PHP ÊõÓïÖУ¬¸ùĿ¼½«ÊÇÈ«¾ÖÃû³Æ¿Õ¼ä£¬Â·¾¶Ãû·ûºÅ½«ÊÇ - ->¡£Òò¶ø£¬Ãû³Æ $cart->items ºÍ $another_cart->items ÃüÃûÁËÁ½¸ö²»Í¬µÄ - ±äÁ¿¡£×¢Òâ±äÁ¿ $cart->items£¬²»ÊÇ $cart->$items£¬ÄÇÊÇÒòΪÔÚ PHP ÖÐÒ»¸ö±äÁ¿ - ÃûÖ»ÓÐÒ»¸öµ¥¶ÀµÄÃÀÔª·ûºÅ¡£ + $cart ºÍ $another_cart ¶¼Óз½·¨ add_item(), remove_item() ºÍÒ»¸ö items + ±äÁ¿¡£ËüÃǶ¼ÊÇÃ÷ÏԵĺ¯ÊýºÍ±äÁ¿¡£Äã¿ÉÒÔ°ÑËüÃǵ±×÷ÎļþϵͳÖеÄijЩÀàËÆÄ¿Â¼µÄ¶«Î÷À´¿¼ÂÇ¡£ÔÚÎļþϵͳÖУ¬Äã¿ÉÒÔÓµÓÐÁ½¸ö²»Í¬µÄ + README.TXT Îļþ£¬Ö»Òª²»ÔÚÏàͬµÄĿ¼ÖС£ÕýÈç´ÓΪÁ˸ùĿ¼·ÃÎÊÿ¸öÎļþÄãÐèÒªÊäÈë¸ÃÎļþµÄÍêÕûµÄ·¾¶ÃûÒ»Ñù£¬Äã±ØÐëÖ¸¶¨ÐèÒªµ÷Óõĺ¯ÊýµÄÍêÕûÃû³Æ£ºÔÚ + PHP ÊõÓïÖУ¬¸ùĿ¼½«ÊÇÈ«¾ÖÃû³Æ¿Õ¼ä£¬Â·¾¶Ãû·ûºÅ½«ÊÇ + ->¡£Òò¶ø£¬Ãû³Æ $cart->items ºÍ $another_cart->items + ÃüÃûÁËÁ½¸ö²»Í¬µÄ±äÁ¿¡£×¢Òâ±äÁ¿ $cart->items£¬²»ÊÇ + $cart->$items£¬ÄÇÊÇÒòΪÔÚ PHP ÖÐÒ»¸ö±äÁ¿ÃûÖ»ÓÐÒ»¸öµ¥¶ÀµÄÃÀÔª·ûºÅ¡£ </para> <informalexample> <programlisting role="php"> <![CDATA[ -// correct, single $ +// ÕýÈ·£¬Ö»ÓÐÒ»¸ö $ $cart->items = array("10" => 1); -// invalid, because $cart->$items becomes $cart->"" +// ²»ÕýÈ·£¬ÒòΪ $cart->$items ±ä³ÉÁË $cart->"" $cart->$items = array("10" => 1); -// correct, but may or may not be what was intended: -// $cart->$myvar becomes $cart->items +// ÕýÈ·£¬µ«¿ÉÄܲ»ÊÇÏëÒªµÄ½á¹û£º +// $cart->$myvar ±ä³ÉÁË $cart->items $myvar = 'items'; $cart->$myvar = array("10" => 1); ]]> @@ -165,18 +160,17 @@ </informalexample> <para> - ÔÚÒ»¸öÀàµÄ¶¨ÒåÄÚ²¿£¬ÄãÎÞ·¨µÃ֪ʹÓúÎÖÖÃû³ÆµÄ¶ÔÏóÊÇ¿ÉÒÔ·ÃÎʵģºÔÚ±àд Cart Ààʱ£¬²¢ - ²»ÖªµÀÖ®ºó¶ÔÏóµÄÃû³Æ½«»áÃüÃûΪ $cart »òÕß $another_cart¡£Òò¶øÄã²»ÄÜÔÚÀàÖÐʹÓà - $cart->items¡£È»¶øÎªÁËÀඨÒåµÄÄÚ²¿·ÃÎÊ×ÔÉíµÄº¯ÊýºÍ±äÁ¿£¬ ¿ÉÒÔʹÓÃα±äÁ¿ $this - À´´ïµ½Õâ¸öÄ¿µÄ¡£$this ±äÁ¿¿ÉÒÔÀí½âΪ¡®ÎÒ×Ô¼ºµÄ¡¯»òÕß¡®µ±Ç°¶ÔÏ󡯡£Òò¶ø - ¡®$this->>items[$artnr] += $num¡¯ ¿ÉÒÔÀí½âΪ¡®ÎÒ×Ô¼ºµÄÎïÆ·Êý×éµÄ $artnr ¼ÆÊýÆ÷¼Ó $num¡¯ - »òÕß ¡®ÔÚµ±Ç°¶ÔÏóµÄÎïÆ·Êý×éµÄ $artnr ¼ÆÊýÆ÷¼Ó $num¡¯¡£ + ÔÚÒ»¸öÀàµÄ¶¨ÒåÄÚ²¿£¬ÄãÎÞ·¨µÃ֪ʹÓúÎÖÖÃû³ÆµÄ¶ÔÏóÊÇ¿ÉÒÔ·ÃÎʵģºÔÚ±àд Cart + Ààʱ£¬²¢²»ÖªµÀÖ®ºó¶ÔÏóµÄÃû³Æ½«»áÃüÃûΪ $cart »òÕß $another_cart¡£Òò¶øÄã²»ÄÜÔÚÀàÖÐʹÓà + $cart->items¡£È»¶øÎªÁËÀඨÒåµÄÄÚ²¿·ÃÎÊ×ÔÉíµÄº¯ÊýºÍ±äÁ¿£¬ ¿ÉÒÔʹÓÃα±äÁ¿ $this + À´´ïµ½Õâ¸öÄ¿µÄ¡£$this ±äÁ¿¿ÉÒÔÀí½âΪ¡°ÎÒ×Ô¼ºµÄ¡±»òÕß¡°µ±Ç°¶ÔÏ󡱡£Òò¶ø + '$this->>items[$artnr] += $num' ¿ÉÒÔÀí½âΪ¡°ÎÒ×Ô¼ºµÄÎïÆ·Êý×éµÄ $artnr ¼ÆÊýÆ÷¼Ó + $num¡±»òÕß¡°ÔÚµ±Ç°¶ÔÏóµÄÎïÆ·Êý×éµÄ $artnr ¼ÆÊýÆ÷¼Ó $num¡±¡£ </para> <note> <para> - ÓÐһЩ²»´íµÄº¯ÊýÓÃÀ´´¦ÀíÀàºÍ¶ÔÏó¡£ÄãÓ¦¸Ã¹Ø×¢Ò»Ï - <link linkend="ref.classobj">Àà/¶ÔÏó º¯Êý</link>¡£ + ÓÐһЩ²»´íµÄº¯ÊýÓÃÀ´´¦ÀíÀàºÍ¶ÔÏó¡£ÄãÓ¦¸Ã¹Ø×¢Ò»ÏÂ<link linkend="ref.classobj">Àà/¶ÔÏóº¯Êý¿â</link>¡£ </para> </note> </sect1> @@ -185,13 +179,7 @@ <title><literal>¼Ì³Ð</literal></title> <para> - ͨ³£ÄãÐèÒªÕâÑùһЩÀ࣬ÕâЩÀàÓëÆäËüÏÖÓеÄÀàÓµÓÐÏàͬ±äÁ¿ºÍº¯Êý¡£Êµ¼ÊÉÏ£¬¶¨ÒåÒ»¸ö - ͨÓÃÀ࣬ÓÃÓÚÄãËùÓеÄÏîÄ¿£¬²¢ÇÒ²»¶Ï·á¸»Õâ¸öÀàÒÔÊÊÓ¦ÄãµÄÿ¸ö¾ßÌåÏîÄ¿£¬½«ÊÇÒ»¸ö²»´í - µÄÁ·Ï°¡£ÎªÁËʹÕâÒ»µã±äµÃ¸ü¼ÓÈÝÒ×£¬Àà¿ÉÒÔ´ÓÆäËüµÄÀàÖÐÀ©Õ¹³öÀ´¡£À©Õ¹»òÅÉÉú³öÀ´µÄÀà - ÓµÓÐÆä»ùÀࣨthis is called 'inheritance' despite the fact that nobody died£© - µÄËùÓбäÁ¿ºÍº¯Êý£¬²¢°üº¬ËùÓÐÄãÔÚÅÉÉúÀàÖж¨ÒåµÄ²¿·Ö¡£ÀàÖеÄÔªËØ²»¿ÉÄܼõÉÙ£¬¾ÍÊÇ˵£¬ - ²»¿ÉÒÔ×¢ÏúÈκδæÔڵĺ¯Êý»òÕß±äÁ¿¡£Ò»¸öÀ©³äÀà×ÜÊÇÒÀÀµÒ»¸öµ¥¶ÀµÄ»ùÀ࣬Ҳ¾ÍÊÇ˵£¬ - ¶à¼Ì³ÐÊDz»Ö§³ÖµÄ¡£Ê¹Óùؼü×Ö¡®extends¡¯À´À©Õ¹Ò»¸öÀà¡£ + ͨ³£ÄãÐèÒªÕâÑùһЩÀ࣬ÕâЩÀàÓëÆäËüÏÖÓеÄÀàÓµÓÐÏàͬ±äÁ¿ºÍº¯Êý¡£Êµ¼ÊÉÏ£¬¶¨ÒåÒ»¸öͨÓÃÀ࣬ÓÃÓÚÄãËùÓеÄÏîÄ¿£¬²¢ÇÒ²»¶Ï·á¸»Õâ¸öÀàÒÔÊÊÓ¦ÄãµÄÿ¸ö¾ßÌåÏîÄ¿£¬½«ÊÇÒ»¸ö²»´íµÄÁ·Ï°¡£ÎªÁËʹÕâÒ»µã±äµÃ¸ü¼ÓÈÝÒ×£¬Àà¿ÉÒÔ´ÓÆäËüµÄÀàÖÐÀ©Õ¹³öÀ´¡£À©Õ¹»òÅÉÉú³öÀ´µÄÀàÓµÓÐÆä»ùÀࣨÕâ³ÆÎª¡°¼Ì³Ð¡±£¬Ö»²»¹ýûÈËËÀ£©µÄËùÓбäÁ¿ºÍº¯Êý£¬²¢°üº¬ËùÓÐÄãÔÚÅÉÉúÀàÖж¨ÒåµÄ²¿·Ö¡£ÀàÖеÄÔªËØ²»¿ÉÄܼõÉÙ£¬¾ÍÊÇ˵£¬²»¿ÉÒÔ×¢ÏúÈκδæÔڵĺ¯Êý»òÕß±äÁ¿¡£Ò»¸öÀ©³äÀà×ÜÊÇÒÀÀµÒ»¸öµ¥¶ÀµÄ»ùÀ࣬Ҳ¾ÍÊÇ˵£¬¶à¼Ì³ÐÊDz»Ö§³ÖµÄ¡£Ê¹Óùؼü×Ö¡°extends¡±À´À©Õ¹Ò»¸öÀà¡£ </para> <informalexample> @@ -211,34 +199,33 @@ </informalexample> <para> - ÉÏÊöʾÀý¶¨ÒåÁËÃûΪ Named_Cart µÄÀ࣬¸ÃÀàÓµÓÐ Cart ÀàµÄËùÓбäÁ¿ºÍº¯Êý£¬¼ÓÉÏ - ¸½¼ÓµÄ±äÁ¿ $owner ºÍÒ»¸ö¸½¼Óº¯Êý set_owner()¡£ÏÖÔÚ£¬ÄãÒÔÕý³£µÄ·½Ê½´´½¨ÁËÒ» - ¸ö named cart£¬²¢ÇÒ¿ÉÒÔÉèÖò¢È¡µÃ carts µÄËùÓÐÕß¡£¶øÕý³£µÄ cart ÀàµÄº¯ÊýÒÀ¾É - ¿ÉÒÔÔÚ named carts ÖÐʹÓ㺠+ ÉÏÊöʾÀý¶¨ÒåÁËÃûΪ Named_Cart µÄÀ࣬¸ÃÀàÓµÓÐ Cart + ÀàµÄËùÓбäÁ¿ºÍº¯Êý£¬¼ÓÉϸ½¼ÓµÄ±äÁ¿ $owner ºÍÒ»¸ö¸½¼Óº¯Êý + set_owner()¡£ÏÖÔÚ£¬ÄãÒÔÕý³£µÄ·½Ê½´´½¨ÁËÒ»¸öÓÐÃû×ֵĹºÎï³µ£¬²¢ÇÒ¿ÉÒÔÉèÖò¢È¡µÃ¸Ã¹ºÎï³µµÄÖ÷ÈË¡£¶øÕý³£µÄ¹ºÎï³µÀàµÄº¯ÊýÒÀ¾É¿ÉÒÔÔÚÓÐÃû×ֵĹºÎï³µÀàÖÐʹÓ㺠</para> <informalexample> <programlisting role="php"> <![CDATA[ -$ncart = new Named_Cart; // Create a named cart -$ncart->set_owner("kris"); // Name that cart -print $ncart->owner; // print the cart owners name -$ncart->add_item("10", 1); // (inherited functionality from cart) +$ncart = new Named_Cart; // н¨Ò»¸öÓÐÃû×ֵĹºÎï³µ +$ncart->set_owner("kris"); // ¸ø¸Ã¹ºÎï³µÃüÃû +print $ncart->owner; // Êä³ö¸Ã¹ºÎï³µÖ÷È˵ÄÃû×Ö +$ncart->add_item("10", 1); // £¨´Ó¹ºÎï³µÀàÖм̳ÐÀ´µÄ¹¦ÄÜ£© ]]> </programlisting> </informalexample> <para> - Õâ¸öÒ²¿ÉÒÔ½Ð×ö¡°¸¸£×Ó¡±¹ØÏµ¡£Äã´´½¨Ò»¸öÀ࣬¸¸À࣬²¢Ê¹Óà <literal>extends</literal> - À´´´½¨Ò»¸ö <emphasis>»ùÓÚ</emphasis> ¸¸ÀàµÄÐÂÀࣺ×ÓÀà¡£ÄãÉõÖÁ¿ÉÒÔʹÓÃÕâ¸öеÄ×ÓÀàÀ´ - ´´½¨ÁíÍâÒ»¸ö»ùÓÚÕâ¸ö×ÓÀàµÄÀà¡£ + Õâ¸öÒ²¿ÉÒÔ½Ð×ö¡°¸¸£×Ó¡±¹ØÏµ¡£Äã´´½¨Ò»¸öÀ࣬¸¸À࣬²¢Ê¹Óà <literal>extends</literal> + À´´´½¨Ò»¸ö<emphasis>»ùÓÚ</emphasis>¸¸ÀàµÄÐÂÀࣺ×ÓÀà¡£ÄãÉõÖÁ¿ÉÒÔʹÓÃÕâ¸öеÄ×ÓÀàÀ´´´½¨ÁíÍâÒ»¸ö»ùÓÚÕâ¸ö×ÓÀàµÄÀà¡£ </para> <note> <para> - ÀàÖ»ÓÐÔÚ¶¨Òåºó²Å¿ÉÒÔʹÓã¡Èç¹ûÄãÐëÒªÀà <literal>Named_Cart</literal> ¼Ì³ÐÀà <literal>Cart</literal>£¬ - Äã±ØÐëÊ×Ïȶ¨Òå <literal>Cart</literal> Àà¡£Èç¹ûÄãÐëÒª´´½¨ÁíÒ»¸ö»ùÓÚ <literal>Named_Cart</literal> Àà - µÄ <literal>Yellow_named_cart </literal> À࣬Äã±ØÐëÊ×Ïȶ¨Òå <literal>Named_Cart</literal> Àà¡£ - ¼ò½ÝµÄ˵£ºÀඨÒåµÄ˳ÐòÊǷdz£ÖØÒªµÄ¡£ + ÀàÖ»ÓÐÔÚ¶¨Òåºó²Å¿ÉÒÔʹÓã¡Èç¹ûÄãÐèÒªÀà <literal>Named_Cart</literal> + ¼Ì³ÐÀà <literal>Cart</literal>£¬Äã±ØÐëÊ×Ïȶ¨Òå <literal>Cart</literal> + Àà¡£Èç¹ûÄãÐëÒª´´½¨ÁíÒ»¸ö»ùÓÚ <literal>Named_Cart</literal> + ÀàµÄ <literal>Yellow_named_cart </literal> À࣬Äã±ØÐëÊ×Ïȶ¨Òå + <literal>Named_Cart</literal> Àà¡£¼ò½ÝµÄ˵£ºÀඨÒåµÄ˳ÐòÊǷdz£ÖØÒªµÄ¡£ </para> </note> </sect1> @@ -248,21 +235,21 @@ <caution> <simpara> - PHP 3 ºÍ PHP4 µÄ¹¹Ô캯ÊýÓÐËù²»Í¬¡£PHP 4 µÄÓïÒå¸ü¿ÉÈ¡¡£ + PHP 3 ºÍ PHP4 µÄ¹¹Ô캯ÊýÓÐËù²»Í¬¡£PHP 4 µÄÓïÒå¸ü¿ÉÈ¡¡£ </simpara> </caution> <para> - ¹¹Ô캯ÊýÊÇÀàÖеÄÒ»¸öÌØÊ⺯Êý£¬µ±Ê¹Óà <literal>new</literal> ²Ù×÷·û - ´´½¨Ò»¸öÀàµÄʵÀýʱ£¬¹¹Ô캯Êý½«»á×Ô¶¯µ÷Óá£PHP 3 ÖУ¬µ±º¯ÊýÓÚÀàͬÃûʱ£¬ - Õâ¸öº¯Êý½«³ÉΪ¹¹Ô캯Êý¡£PHP 4 £¬ÀàÖж¨ÒåµÄº¯ÊýÓëÀàͬÃûʱ£¬Õâ¸öº¯Êý½« - ³ÉΪһ¸ö¹¹Ô캯Êý£Çø±ðºÜ΢Ãµ«·Ç³£¹Ø¼ü£¨¼ûÏÂÎÄ£©¡£ + ¹¹Ô캯ÊýÊÇÀàÖеÄÒ»¸öÌØÊ⺯Êý£¬µ±Ê¹Óà <literal>new</literal> + ²Ù×÷·û´´½¨Ò»¸öÀàµÄʵÀýʱ£¬¹¹Ô캯Êý½«»á×Ô¶¯µ÷Óá£PHP 3 + ÖУ¬µ±º¯ÊýÓëÀàͬÃûʱ£¬Õâ¸öº¯Êý½«³ÉΪ¹¹Ô캯Êý¡£PHP 4 + ÖУ¬ÔÚÀàÀﶨÒåµÄº¯ÊýÓëÀàͬÃûʱ£¬Õâ¸öº¯Êý½«³ÉΪһ¸ö¹¹Ô캯Êý £ Çø±ðºÜ΢Ãµ«·Ç³£¹Ø¼ü£¨¼ûÏÂÎÄ£©¡£ </para> <informalexample> <programlisting role="php"> <![CDATA[ -// Works in PHP 3 and PHP 4. +// PHP 3 ºÍ PHP 4 Öж¼ÄÜÓà class Auto_Cart extends Cart { function Auto_Cart() @@ -275,16 +262,14 @@ </informalexample> <para> - ÉÏÎ͍ÒåÁËÒ»¸ö Auto_Cart À࣬¼´ Cart Àà¼ÓÉÏÒ»¸ö¹¹Ô캯Êý£¬µ±Ã¿´ÎʹÓà "new" ´´½¨ - Ò»¸öÐ嵀 Auto_Cart ÀàʵÀýʱ£¬¹¹Ô캯Êý½«×Ô¶¯µ÷Óò¢½«Ò»¼þÉÌÆ·µÄÊýÄ¿³õʼ»¯Îª ¡°10¡±¡£ - ¹¹Ô캯Êý¿ÉÒÔʹÓòÎÊý£¬¶øÇÒÕâЩ²ÎÊý¿ÉÑ¡µÄ£¬ËüÃÇ¿ÉÒÔʹ¹¹Ô캯Êý¸ü¼ÓÓÐÓá£ÎªÁËÒÀÈ»¿ÉÒÔ - ²»´ø²ÎÊýµÄʹÓÃÀ࣬ËùÓй¹Ô캯ÊýµÄ²ÎÊýÓ¦¸ÃÌṩĬÈÏÖµ£¬Ê¹Æä¿ÉÑ¡¡£ + ÉÏÎ͍ÒåÁËÒ»¸ö Auto_Cart À࣬¼´ Cart Àà¼ÓÉÏÒ»¸ö¹¹Ô캯Êý£¬µ±Ã¿´ÎʹÓá°new¡±´´½¨Ò»¸öÐ嵀 Auto_Cart + ÀàʵÀýʱ£¬¹¹Ô캯Êý½«×Ô¶¯µ÷Óò¢½«Ò»¼þÉÌÆ·µÄÊýÄ¿³õʼ»¯Îª¡°10¡±¡£¹¹Ô캯Êý¿ÉÒÔʹÓòÎÊý£¬¶øÇÒÕâЩ²ÎÊý¿ÉÒÔÊÇ¿ÉÑ¡µÄ£¬ËüÃÇ¿ÉÒÔʹ¹¹Ô캯Êý¸ü¼ÓÓÐÓá£ÎªÁËÒÀÈ»¿ÉÒÔ²»´ø²ÎÊýµØÊ¹ÓÃÀ࣬ËùÓй¹Ô캯ÊýµÄ²ÎÊýÓ¦¸ÃÌṩĬÈÏÖµ£¬Ê¹Æä¿ÉÑ¡¡£ </para> <informalexample> <programlisting role="php"> <![CDATA[ -// Works in PHP 3 and PHP 4. +// PHP 3 ºÍ PHP 4 Öж¼ÄÜÓà class Constructor_Cart extends Cart { function Constructor_Cart($item = "10", $num = 1) @@ -293,11 +278,11 @@ } } -// Shop the same old boring stuff. +// ÂòЩͬÑùÎÞÁĵÄÀÏ»õ $default_cart = new Constructor_Cart; -// Shop for real... +// ÂòЩж«Î÷... $different_cart = new Constructor_Cart("20", 17); ]]> @@ -305,14 +290,14 @@ </informalexample> <para> - ÄãÒ²¿ÉÒÔʹÓà <literal>@</literal> ²Ù×÷·ûÀ´ <emphasis>Ïû³ý</emphasis> ·¢ÉúÔÚ¹¹Ô캯ÊýÖеĴíÎó¡£e.g. - <literal>@new</literal>. + ÄãÒ²¿ÉÒÔʹÓà <literal>@</literal> + ²Ù×÷·ûÀ´<emphasis>Ïû³ý</emphasis>·¢ÉúÔÚ¹¹Ô캯ÊýÖеĴíÎó¡£ÀýÈç <literal>@new</literal>¡£ </para> <caution> <simpara> - PHP 3£¬ÅÉÉúÀàºÍ¹¹Ô캯ÊýÓÐÐí¶àÏÞÖÆ¡£×ÐϸÔĶÁÏÂÁз¶ÀýÒÔÀí½âÕâЩÏÞÖÆ¡£ + PHP 3 ÖÐÅÉÉúÀàºÍ¹¹Ô캯ÊýÓÐÐí¶àÏÞÖÆ¡£×ÐϸÔĶÁÏÂÁз¶ÀýÒÔÀí½âÕâЩÏÞÖÆ¡£ </simpara> </caution> @@ -335,20 +320,21 @@ } } -// no constructor is being called in PHP 3. +// PHP 3 ÖÐûÓй¹Ô캯Êý±»µ÷Óà $b = new B; ]]> </programlisting> </informalexample> <para> - PHP 3£¬ÔÚÉÏÃæµÄʾÀýÖн«²»»áÓй¹Ô캯Êý±»µ÷Óá£PHP 3 µÄ¹æÔòÊÇ£º¡°¹¹Ô캯ÊýÊÇÓëÀà - ͬÃûµÄº¯Êý¡±¡£ÕâÀÀàµÄÃû×ÖÊÇ B£¬µ«ÊÇÀà B ÖÐûÓк¯Êý B()¡£Ê²Ã´Ò²²»»á·¢Éú¡£ + PHP 3 ÖУ¬ÔÚÉÏÃæµÄʾÀýÖн«²»»áÓй¹Ô캯Êý±»µ÷Óá£PHP 3 + µÄ¹æÔòÊÇ£º¡°¹¹Ô캯ÊýÊÇÓëÀàͬÃûµÄº¯Êý¡±¡£ÕâÀÀàµÄÃû×ÖÊÇ B£¬µ«ÊÇÀà B + ÖÐûÓк¯Êý B()¡£Ê²Ã´Ò²²»»á·¢Éú¡£ </para> <para> - PHP 4 ÐÞÕýÁËÕâ¸öÎÊÌ⣬²¢½éÉÜÁËÁíÍâµÄйæÔò£ºÈç¹ûÒ»¸öÀàûÓй¹Ô캯Êý£¬Èç¹û¸¸ÀàÓй¹Ôì - º¯ÊýµÄ»°£¬¸¸ÀàµÄ¹¹Ô캯Êý½«»á±»µ÷Óá£PHP 4 ÖУ¬ÉÏÃæµÄÀý×Ó½«»áÊä³ö¡°I am the constructor of A.<br>¡±¡£ + PHP 4 ÐÞÕýÁËÕâ¸öÎÊÌ⣬²¢½éÉÜÁËÁíÍâµÄйæÔò£ºÈç¹ûÒ»¸öÀàûÓй¹Ô캯Êý£¬Èç¹û¸¸ÀàÓй¹Ô캯ÊýµÄ»°£¬¸¸ÀàµÄ¹¹Ô캯Êý½«»á±»µ÷Óá£PHP 4 + ÖУ¬ÉÏÃæµÄÀý×Ó½«»áÊä³ö¡°I am the constructor of A.<br>¡±¡£ </para> <informalexample> @@ -376,47 +362,49 @@ } } -// This will call B() as a constructor. +// µ÷Óà B() ×÷Ϊ¹¹Ô캯Êý $b = new B; ]]> </programlisting> </informalexample> <para> - Èç¹ûÊÇ PHP 3£¬A ÀàÖеĺ¯Êý B() ½«Á¢¼´³ÉΪÀà B ÖеĹ¹Ô캯Êý£¬ËäÈ»²¢²»ÊÇÓÐÒâÈç´Ë¡£ - PHP 3 ÖеĹæÔòÊÇ£º¡°¹¹Ô캯ÊýÊÇÓëÀàͬÃûµÄº¯Êý¡±¡£PHP 3 ²¢²»¹ØÐĺ¯ÊýÊDz»ÊÇÔÚ B Àà - Öж¨ÒåµÄ£¬»òÕßÊÇ·ñÒѾ±»¼Ì³Ð¡£ + Èç¹ûÊÇ PHP 3£¬Àà A Öеĺ¯Êý B() ½«Á¢¼´³ÉΪÀà B + ÖеĹ¹Ô캯Êý£¬ËäÈ»²¢²»ÊÇÓÐÒâÈç´Ë¡£PHP 3 + ÖеĹæÔòÊÇ£º¡°¹¹Ô캯ÊýÊÇÓëÀàͬÃûµÄº¯Êý¡±¡£PHP 3 + ²¢²»¹ØÐĺ¯ÊýÊDz»ÊÇÔÚÀà B + Öж¨ÒåµÄ£¬»òÕßÊÇ·ñÒѾ±»¼Ì³Ð¡£ </para> <para> - PHP 4 ÐÞ¸ÄÁ˹æÔò£º¡°¹¹Ô캯ÊýÓ붨ÒåÆä×ÔÉíµÄÀàͬÃû¡±¡£Òò¶øÔÚ PHP 4 ÖУ¬Àà B ½« - ²»»áÓÐÊôÓÚ×ÔÉíµÄ¹¹Ô캯Êý£¬²¢ÇÒ¸¸ÀàµÄ¹¹Ô캯Êý½«»á±»µ÷Ó㬴òÓ¡³ö - ¡°I am the constructor of A.<br>¡±¡£ - - [ÒëÕß×¢,by Verdana: ÕâÀïËÆºõÓÐÎÊÌ⣬ʵ¼ÊÊä³öµÄ½á¹û£¬²¢²»ÏóÕâÀï˵µÄÄÇÑù¡£Êµ¼ÊÊä³öµÄ - ÄÚÈÝÊÇ¡°I am a regular function named B in class A......¡±£¬Êä³öµÄÊÇ B() µÄÄÚÈÝ - Ò²¾ÍÊÇ˵£¬Àý×ÓÖеÄ×¢ÊÍ¡°This will call B() as a constructor¡±ÊÇÕýÈ·µÄ¡£Èç¹û´Ó A Àà - ÖÐÒÆ³ýº¯Êý B()£¬ÄÇô½«Êä³ö A() µÄÄÚÈÝ¡£] + PHP 4 ÐÞ¸ÄÁ˹æÔò£º¡°¹¹Ô캯ÊýÓ붨ÒåÆä×ÔÉíµÄÀàͬÃû¡±¡£Òò¶øÔÚ PHP 4 + ÖУ¬Àà B ½«²»»áÓÐÊôÓÚ×ÔÉíµÄ¹¹Ô캯Êý£¬²¢ÇÒ¸¸ÀàµÄ¹¹Ô캯Êý½«»á±»µ÷Óã¬Êä³ö¡°I + am the constructor of A.<br>¡±¡£ + </para> + <para> + ¡¾ÒëÕß×¢¡¿<!-- By Verdana -->ÕâÀïËÆºõÓÐÎÊÌ⣬ʵ¼ÊÊä³öµÄ½á¹û£¬²¢²»ÏóÕâÀï˵µÄÄÇÑù¡£Êµ¼ÊÊä³öµÄÄÚÈÝÊÇ¡°I + am a regular function named B in class A......¡±£¬Êä³öµÄÊÇ B() + µÄÄÚÈÝ¡£Ò²¾ÍÊÇ˵£¬Àý×ÓÖеÄ×¢ÊÍ¡°This will call B() as a constructor¡±ÊÇÕýÈ·µÄ¡£Èç¹û´Ó A + ÀàÖÐÒÆ³ýº¯Êý B()£¬ÄÇô½«Êä³ö A() µÄÄÚÈÝ¡£ </para> <caution> <simpara> - ²»¹ÜÊÇ PHP 3 »¹ÊÇ PHP 4 ¶¼²»ÄÜ´ÓÅÉÉúÀàµÄ¹¹Ô캯ÊýÖÐ×Ô¶¯µ÷ÓûùÀàµÄ¹¹Ô캯Êý¡£ - It is your reponsibility to prapagater the call to constructors - upstream where appropriate. + ²»¹ÜÊÇ PHP 3 »¹ÊÇ PHP 4 + ¶¼²»»á´ÓÅÉÉúÀàµÄ¹¹Ô캯ÊýÖÐ×Ô¶¯µ÷ÓûùÀàµÄ¹¹Ô캯Êý¡£Ç¡µ±µØÖð´Îµ÷ÓÃÉÏÒ»¼¶µÄ¹¹Ô캯ÊýÊÇÓû§µÄÔðÈΡ£ </simpara> </caution> <note> <simpara> - PHP 3 »òÕß PHP 4 Öж¼Ã»ÓÐÎö¹¹º¯Êý¡£Äã¿ÉÒÔʹÓà <function>register_shutdown_function</function> - º¯ÊýÀ´Ä£Äâ¶àÊýÎö¹¹º¯ÊýµÄЧ¹û¡£ + PHP 3 »òÕß PHP 4 Öж¼Ã»ÓÐÎö¹¹º¯Êý¡£Äã¿ÉÒÔʹÓà <function>register_shutdown_function</function> + º¯ÊýÀ´Ä£Äâ¶àÊýÎö¹¹º¯ÊýµÄЧ¹û¡£ </simpara> </note> <para> - µ±¶ÔÏó±»Ïú»Ù£¬Ê¹Óà <function>unset</function> »òÕß¼òµ¥µÄÍÑÀ뷶Χ£¬Îö¹¹º¯Êý¶¼»á×Ô¶¯µ÷Óᣠ- µ« PHP ÖÐûÓÐÎö¹¹º¯Êý¡£ + Îö¹¹º¯ÊýÊÇÒ»ÖÖµ±¶ÔÏó±»Ïú»Ùʱ£¬ÎÞÂÛʹÓÃÁË <function>unset</function> + »òÕß¼òµ¥µÄÍÑÀ뷶Χ£¬¶¼»á±»×Ô¶¯µ÷Óõĺ¯Êý¡£µ« PHP ÖÐûÓÐÎö¹¹º¯Êý¡£ </para> </sect1> @@ -430,8 +418,8 @@ </caution> <para> - ÓÐʱ£¬ÔÚûÓÐÉùÃ÷ÈκÎʵÀýµÄÇé¿öÏ·ÃÎÊÀàÖеĺ¯Êý»òÕß»ùÀàÖеĺ¯ÊýºÍ±äÁ¿ºÜÓÐÓô¦¡£ - ¶ø :: ²Ù×÷·û¼´ÓÃÓÚ´ËÇé¿ö¡£ + ÓÐʱ£¬ÔÚûÓÐÉùÃ÷ÈκÎʵÀýµÄÇé¿öÏ·ÃÎÊÀàÖеĺ¯Êý»òÕß»ùÀàÖеĺ¯ÊýºÍ±äÁ¿ºÜÓÐÓô¦¡£¶ø + :: ÔËËã·û¼´ÓÃÓÚ´ËÇé¿ö¡£ </para> <informalexample> @@ -454,15 +442,14 @@ } } -// there is no object of class A. -// this will print +// A ÀàûÓжÔÏó£¬Õ⽫Êä³ö // I am the original function A::example().<br> A::example(); -// create an object of class B. +// ½¨Á¢Ò»¸ö B ÀàµÄ¶ÔÏó $b = new B; -// this will print +// Õ⽫Êä³ö // I am the redefined function B::example().<br> // I am the original function A::example().<br> $b->example(); @@ -471,27 +458,26 @@ </informalexample> <para> - ÉÏÃæµÄÀý×Óµ÷ÓÃÁË A ÀàµÄº¯Êý example()£¬µ«ÊÇÕâÀï²¢²»´æÔÚ A ÀàµÄ¶ÔÏó£¬ - Òò´Ë²»ÄÜÕâÑùÓà $a->example() »òÕßÀàËÆµÄ·½·¨µ÷Óà example()¡£·´¶øÎÒÃÇ - ½« example() ×÷Ϊһ¸öÀຯÊýÀ´µ÷Óã¬Ò²¾ÍÊÇ˵£¬×÷Ϊһ¸öÀà×ÔÉíµÄº¯ÊýÀ´µ÷Óà - £¬¶ø²»ÊÇÕâ¸öÀàµÄÈκζÔÏó¡£ + ÉÏÃæµÄÀý×Óµ÷ÓÃÁË A ÀàµÄº¯Êý example()£¬µ«ÊÇÕâÀï²¢²»´æÔÚ A + ÀàµÄ¶ÔÏó£¬Òò´Ë²»ÄÜÕâÑùÓà $a->example() »òÕßÀàËÆµÄ·½·¨µ÷Óà + example()¡£·´¶øÎÒÃǽ« example() + ×÷Ϊһ¸öÀຯÊýÀ´µ÷Óã¬Ò²¾ÍÊÇ˵£¬×÷Ϊһ¸öÀà×ÔÉíµÄº¯ÊýÀ´µ÷Ó㬶ø²»ÊÇÕâ¸öÀàµÄÈκζÔÏó¡£ </para> <para> - ÕâÀïÓÐÀຯÊý£¬µ«Ã»ÓÐÀàµÄ±äÁ¿¡£Êµ¼ÊÉÏ£¬ÔÚµ÷Óú¯ÊýʱÍêȫûÓÐÈκζÔÏó¡£Òò¶ø - Ò»¸öÀàµÄº¯Êý¿ÉÒÔ²»Ê¹ÓÃÈκζÔÏ󣨵«¿ÉÒÔʹÓþֲ¿»òÕßÈ«¾Ö±äÁ¿£©£¬²¢ÇÒ¿ÉÒÔ¸ù±¾ - ²»Ê¹Óà $this ±äÁ¿¡£ + ÕâÀïÓÐÀຯÊý£¬µ«Ã»ÓÐÀàµÄ±äÁ¿¡£Êµ¼ÊÉÏ£¬ÔÚµ÷Óú¯ÊýʱÍêȫûÓÐÈκζÔÏó¡£Òò¶øÒ»¸öÀàµÄº¯Êý¿ÉÒÔ²»Ê¹ÓÃÈκζÔÏ󣨵«¿ÉÒÔʹÓþֲ¿»òÕßÈ«¾Ö±äÁ¿£©£¬²¢ÇÒ¿ÉÒÔ¸ù±¾²»Ê¹Óà $this ±äÁ¿¡£ </para> <para> - ÉÏÃæµÄÀý×ÓÖУ¬Àà B ÖØÐ¶¨ÒåÁ˺¯Êý example()¡£A ÀàÖÐÔʼ¶¨ÒåµÄº¯Êý example() - ½«±»ÆÁ±Î²¢ÇÒ²»ÔÙÉúЧ£¬³ý·ÇÄãʹÓà :: ²Ù×÷·ûÀ´·ÃÎÊ A ÀàÖÐµÄ example() º¯Êý¡£ - È磺 A::example() £¨Êµ¼ÊÉÏ£¬Äã±ØÐë±àд parent::example()£¬ÏÂÒ»Õ½éÉܸÃÄÚÈÝ¡££© + ÉÏÃæµÄÀý×ÓÖУ¬Àà B ÖØÐ¶¨ÒåÁ˺¯Êý example()¡£A + ÀàÖÐÔʼ¶¨ÒåµÄº¯Êý example() + ½«±»ÆÁ±Î²¢ÇÒ²»ÔÙÉúЧ£¬³ý·ÇÄãʹÓà :: + ÔËËã·ûÀ´·ÃÎÊ A ÀàÖÐµÄ example() º¯Êý¡£È磺A::example()£¨Êµ¼ÊÉÏ£¬Ó¦¸ÃдΪ + parent::example()£¬ÏÂÒ»Õ½éÉܸÃÄÚÈÝ¡££© </para> <para> - ¾Í´Ë¶øÂÛ£¬¶ÔÓÚµ±Ç°¶ÔÏó£¬Ëü¿ÉÄÜÓжÔÏó±äÁ¿¡£Òò¶ø£¬Äã¿ÉÒÔÔÚ¶ÔÏóº¯ÊýµÄÄÚ²¿ - ʹÓà $this ºÍ¶ÔÏó±äÁ¿¡£ + ¾Í´Ë¶øÂÛ£¬¶ÔÓÚµ±Ç°¶ÔÏó£¬Ëü¿ÉÄÜÓжÔÏó±äÁ¿¡£Òò¶ø£¬Äã¿ÉÒÔÔÚ¶ÔÏóº¯ÊýµÄÄÚ²¿Ê¹Óà $this ºÍ¶ÔÏó±äÁ¿¡£ </para> </sect1> @@ -500,23 +486,15 @@ <title><literal>parent</literal></title> <para> - µ±Äã·¢ÏÖ×Ô¼ºÕýÔÚдһЩ·ÃÎÊ»ùÀàµÄ±äÁ¿ºÍº¯ÊýµÄ´úÂëʱ£¬Õâ³ä·Ö˵Ã÷ÁË - ÄãµÄÅÉÉúÀà·Ç³£µÄ¾«Á·¡¢×¨Òµ¡£ + Äã¿ÉÄܻᷢÏÖ×Ô¼ºÐ´µÄ´úÂë·ÃÎÊÁË»ùÀàµÄ±äÁ¿ºÍº¯Êý¡£Èç¹ûÅÉÉúÀà·Ç³£¾«Á¶»òÕß»ùÀà·Ç³£×¨Òµ»¯µÄʱºòÓÈÆäÊÇÕâÑù¡£ </para> <para> - Instead of using the literal name of the base class in your - code, you should be using the special name - <literal>parent</literal>, which refers to the name of your - base class as given in the <literal>extends</literal> - declaration of your class. By doing this, you avoid using the - name of your base class in more than one place. Should - your inheritance tree change during implementation, the - change is easily made by simply changing the - <literal>extends</literal> declaration of your class. - - ÄãÓ¦¸ÃÔÚ´úÂëÖÐʹÓÃרÓÃÃû³Æ <literal>parent</literal> Ìæ´ú»ùÀàµÄÃû³Æ£¬ - À´·ÃÎÊ + ²»ÒªÓôúÂëÖлùÀàÎÄ×ÖÉϵÄÃû×Ö£¬Ó¦¸ÃÓÃÌØÊâµÄÃû×Ö + <literal>parent</literal>£¬ËüÖ¸µÄ¾ÍÊÇÅÉÉúÀàÔÚ + <literal>extends</literal> ÉùÃ÷ÖÐËùÖ¸µÄ»ùÀàµÄÃû×Ö¡£ÕâÑù×ö¿ÉÒÔ±ÜÃâÔÚ¶à¸öµØ·½Ê¹ÓûùÀàµÄÃû×Ö¡£ + Èç¹û¼Ì³ÐÊ÷ÔÚʵÏֵĹý³ÌÖÐÒªÐ޸ģ¬Ö»Òª¼òµ¥µØÐÞ¸ÄÀàÖÐ + <literal>extends</literal> ÉùÃ÷µÄ²¿·Ö¡£ </para> <informalexample> @@ -541,7 +519,7 @@ $b = new B; -// This will call B::example(), which will in turn call A::example(). +// Õ⽫µ÷Óà B::example()£¬¶øËü»áÈ¥µ÷Óà A::example()¡£ $b->example(); ]]> </programlisting> @@ -549,45 +527,34 @@ </sect1> <sect1 id="language.oop.serialization"> - <title>ÐòÁл¯¶ÔÏó£»á»°ÖеĶÔÏó</title> + <title>ÐòÁл¯¶ÔÏó £ »á»°ÖеĶÔÏó</title> <note> <simpara> - In PHP 3, objects will lose their class association - throughout the process of serialization and unserialization. - The resulting variable is of type object, but has no class - and no methods, thus it is pretty useless (it has become - just like an array with a funny syntax). + ÔÚ PHP 3 ÖУ¬ÔÚÐòÁл¯ºÍ½âÐòÁл¯µÄ¹ý³ÌÖжÔÏó»áʧȥÀàµÄ¹ØÁª¡£½á¹ûµÄ±äÁ¿ÊǶÔÏóÀàÐÍ£¬µ«ÊÇûÓÐÀàºÍ·½·¨£¬Òò´Ë¾ÍûʲôÓÃÁË£¨¾ÍºÃÏñÒ»¸öÓû¬»üµÄÓï·¨¶¨ÒåµÄÊý×éÒ»Ñù£©¡£ </simpara> </note> <caution> <simpara> - The following information is valid for PHP 4 only. + ÒÔÏÂÐÅÏ¢½öÔÚ PHP 4 ÖÐÓÐЧ¡£ </simpara> </caution> <para> - <function>serialize</function> returns a string containing a - byte-stream representation of any value that can be stored in - PHP. <function>unserialize</function> can use this string to - recreate the original variable values. Using serialize to - save an object will save all variables in an object. The - functions in an object will not be saved, only the name of - the class. + <function>serialize</function> + ·µ»ØÒ»¸ö×Ö·û´®£¬°üº¬×Å¿ÉÒÔ´¢´æÓÚ PHP µÄÈκÎÖµµÄ×Ö½ÚÁ÷±íʾ¡£<function>unserialize</function> + ¿ÉÒÔÓôË×Ö·û´®À´Öؽ¨ÔʼµÄ±äÁ¿Öµ¡£ÓÃÐòÁл¯À´±£´æ¶ÔÏó¿ÉÒÔ±£´æ¶ÔÏóÖеÄËùÓбäÁ¿¡£¶ÔÏóÖеĺ¯Êý²»»á±»±£´æ£¬Ö»ÓÐÀàµÄÃû³Æ¡£ </para> <para> - In order to be able to <function>unserialize</function> an - object, the class of that object needs to be defined. That - is, if you have an object $a of class A on page1.php and - serialize this, you'll get a string that refers to class A - and contains all values of variabled contained in $a. If - you want to be able to unserialize this on page2.php, - recreating $a of class A, the definition of class A must - be present in page2.php. This can be done for example - by storing the class defintion of class A in an include - file and including this file in both page1.php and page2.php. + ÒªÄܹ» <function>unserialize</function> + Ò»¸ö¶ÔÏó£¬ÐèÒª¶¨Òå¸Ã¶ÔÏóµÄÀà¡£Ò²¾ÍÊÇ£¬Èç¹ûÐòÁл¯ÁË + page1.php ÖÐÀà A µÄ¶ÔÏó $a£¬½«µÃµ½Ò»¸öÖ¸ÏòÀà A + µÄ×Ö·û´®²¢°üº¬ÓÐËùÓÐ $a ÖбäÁ¿µÄÖµ¡£Èç¹ûÒªÔÚ + page2.php Öн«Æä½âÐòÁл¯£¬Öؽ¨Àà A µÄ¶ÔÏó + $a£¬Ôò page2.php ÖбØÐëÒª³öÏÖÀà A µÄ¶¨Òå¡£Õâ¿ÉÒÔÀýÈçÕâÑùʵÏÖ£¬½«Àà + A µÄ¶¨Òå·ÅÔÚÒ»¸ö°üº¬ÎļþÖУ¬²¢ÔÚ page1.php ºÍ page2.php ¶¼°üº¬´ËÎļþ¡£ </para> <informalexample> @@ -609,91 +576,77 @@ $a = new A; $s = serialize($a); - // store $s somewhere where page2.php can find it. + // ½« $s ´æ·ÅÔÚij´¦Ê¹ page2.php Äܹ»ÕÒµ½ $fp = fopen("store", "w"); fputs($fp, $s); fclose($fp); page2.php: - // this is needed for the unserialize to work properly. + // ΪÁËÕý³£½âÐòÁл¯ÐèÒªÕâÒ»ÐÐ include("classa.inc"); $s = implode("", @file("store")); $a = unserialize($s); - // now use the function show_one() of the $a object. + // ÏÖÔÚ¿ÉÒÔÓà $a ¶ÔÏóµÄ show_one() º¯ÊýÁË $a->show_one(); ]]> </programlisting> </informalexample> <para> - If you are using sessions and use <function>session_register</function> - to register objects, these objects are serialized automatically - at the end of each PHP page, and are unserialized automatically on - each of the following pages. This basically means that these objects - can show up on any of your pages once they become part of your - session. + Èç¹ûÔÚÓûỰ²¢Ê¹ÓÃÁË + <function>session_register</function> + À´×¢²á¶ÔÏó£¬ÕâЩ¶ÔÏó»áÔÚÿ¸ö PHP + Ò³Ãæ½áÊøÊ±±»×Ô¶¯ÐòÁл¯£¬²¢ÔÚ½ÓÏÂÀ´µÄÿ¸öÒ³ÃæÖÐ×Ô¶¯½âÐòÁл¯¡£»ù±¾ÉÏÊÇ˵ÕâЩ¶ÔÏóÒ»µ©³ÉΪ»á»°µÄÒ»²¿·Ö£¬¾ÍÄÜÔÚÈκÎÒ³ÃæÖгöÏÖ¡£ </para> <para> - It is strongly recommended that you include the class - definitions of all such registered objects on all of your - pages, even if you do not actually use these classes on all - of your pages. If you don't and an object is being - unserialized without its class definition being present, it - will lose its class association and become an object of class - <literal>stdClass</literal> without any functions available - at all, that is, it will become quite useless. + Ç¿ÁÒ½¨ÒéÔÚËùÓеÄÒ³ÃæÖж¼°üÀ¨ÕâЩע²áµÄ¶ÔÏóµÄÀàµÄ¶¨Ò壬¼´Ê¹²¢²»ÊÇÔÚËùÓеÄÒ³ÃæÖж¼Óõ½ÁËÕâЩÀà¡£Èç¹ûûÓÐÕâÑù×ö£¬Ò»¸ö¶ÔÏó±»½âÐòÁл¯Á˵«È´Ã»ÓÐÆäÀàµÄ¶¨Ò壬Ëü½«Ê§È¥ÓëÖ®¹ØÁªµÄÀಢ³ÉΪ + <literal>stdClass</literal> + µÄÒ»¸ö¶ÔÏó¶øÍêȫûÓÐÈκοÉÓõĺ¯Êý£¬ÕâÑù¾ÍºÜûÓÐÓô¦¡£ </para> <para> - So if in the example above $a became part of a session by - running <literal>session_register("a")</literal>, you should - include the file <literal>classa.inc</literal> on all of your - pages, not only page1.php and page2.php. + Òò´ËÈç¹ûÔÚÒÔÉϵÄÀý×ÓÖÐ $a ͨ¹ýÔËÐÐ + <literal>session_register("a")</literal> + ³ÉΪÁ˻ỰµÄÒ»²¿·Ö£¬Ó¦¸ÃÔÚËùÓеÄÒ³ÃæÖаüº¬ + <literal>classa.inc</literal> Îļþ£¬¶ø²»Ö»ÊÇ + page1.php ºÍ page2.php¡£ </para> </sect1> <sect1 id="language.oop.magic-functions"> - <title>The magic functions <literal>__sleep</literal> and <literal>__wakeup</literal></title> + <title>ħÊõº¯Êý <literal>__sleep</literal> ºÍ <literal>__wakeup</literal></title> <para> - <function>serialize</function> checks if your class has a function with - the magic name <literal>__sleep</literal>. If so, that function is - being run prior to any serialization. It can clean up the object - and is supposed to return an array with the names of all variables - of that object that should be serialized. + <function>serialize</function> ¼ì²éÀàÖÐÊÇ·ñÓÐħÊõÃû³Æ + <literal>__sleep</literal> + µÄº¯Êý¡£Èç¹ûÕâÑù£¬¸Ãº¯Êý½«ÔÚÈκÎÐòÁл¯Ö®Ç°ÔËÐС£Ëü¿ÉÒÔÇå³ý¶ÔÏó²¢Ó¦¸Ã·µ»ØÒ»¸ö°üº¬ÓиöÔÏóÖÐÓ¦±»ÐòÁл¯µÄËùÓбäÁ¿ÃûµÄÊý×é¡£ </para> <para> - The intended use of <literal>__sleep</literal> is to close any - database connections that object may have, committing pending - data or perform similar cleanup tasks. Also, the function is - useful if you have very large objects which need not be - saved completely. + ʹÓà <literal>__sleep</literal> + µÄÄ¿µÄÊǹرնÔÏó¿ÉÄܾßÓеÄÈκÎÊý¾Ý¿âÁ¬½Ó£¬Ìá½»µÈ´ýÖеÄÊý¾Ý»ò½øÐÐÀàËÆµÄÇå³ýÈÎÎñ¡£´ËÍ⣬Èç¹ûÓзdz£´óµÄ¶ÔÏó¶ø²¢²»ÐèÒªÍêÈ«´¢´æÏÂÀ´Ê±´Ëº¯ÊýÒ²ºÜÓÐÓᣠ</para> <para> - Conversely, <function>unserialize</function> checks for the - presence of a function with the magic name - <literal>__wakeup</literal>. If present, this function can - reconstruct any resources that object may have. + Ïà·´µØ£¬<function>unserialize</function> + ¼ì²é¾ßÓÐħÊõÃû³Æ + <literal>__wakeup</literal> + µÄº¯ÊýµÄ´æÔÚ¡£Èç¹û´æÔÚ£¬´Ëº¯Êý¿ÉÒÔÖØ½¨¶ÔÏó¿ÉÄܾßÓеÄÈκÎ×ÊÔ´¡£ </para> <para> - The intended use of <literal>__wakeup</literal> is to - reestablish any database connections that may have been lost - during serialization and perform other reinitialization - tasks. + ʹÓà <literal>__wakeup</literal> + µÄÄ¿µÄÊÇÖØ½¨ÔÚÐòÁл¯ÖпÉÄܶªÊ§µÄÈκÎÊý¾Ý¿âÁ¬½ÓÒÔ¼°´¦ÀíÆäËüÖØÐ³õʼ»¯µÄÈÎÎñ¡£ </para> </sect1> <sect1 id="language.oop.newref"> - <title>References inside the constructor</title> + <title>¹¹Ô캯ÊýÖеÄÒýÓÃ</title> <para> - Creating references within the constructor can lead to confusing - results. This tutorial-like section helps you to avoid problems. + ÔÚ¹¹Ô캯ÊýÖд´½¨ÒýÓÿÉÄܻᵼÖ»ìÏýµÄ½á¹û¡£±¾½ÚÒԽ̳ÌÐÎʽ°ïÖú±ÜÃâÎÊÌâ¡£ <informalexample> <programlisting role="php"> @@ -702,12 +655,12 @@ { function Foo($name) { - // create a reference inside the global array $globalref + // ÔÚÈ«¾ÖÊý×é $globalref Öн¨Á¢Ò»¸öÒýÓà global $globalref; $globalref[] = &$this; - // set name to passed value + // ½«Ãû×ÖÉ趨Ϊ´«µÝµÄÖµ $this->setName($name); - // and put it out + // ²¢Êä³öÖ® $this->echoName(); } @@ -727,11 +680,15 @@ </para> <para> - Let us check out if there is a difference between - <varname>$bar1</varname> which has been created using - the copy <literal>=</literal> operator and - <varname>$bar2</varname> which has been created using - the reference <literal>=&</literal> operator... + ÏÂÃæÀ´¼ì²éÒ»ÏÂÓÿ½±´ÔËËã·û + <literal>=</literal> + ´´½¨µÄ + <varname>$bar1</varname> + ºÍÓÃÒýÓÃÔËËã·û + <literal>=&</literal> + ´´½¨µÄ + <varname>$bar2</varname> + ÓÐûÓÐÇø±ð... <informalexample> <programlisting role="php"> @@ -740,7 +697,7 @@ $bar1->echoName(); $globalref[0]->echoName(); -/* output: +/* Êä³ö£º set in constructor set in constructor set in constructor */ @@ -749,7 +706,7 @@ $bar2->echoName(); $globalref[1]->echoName(); -/* output: +/* Êä³ö£º set in constructor set in constructor set in constructor */ @@ -758,49 +715,41 @@ </informalexample> </para> <para> - Apparently there is no difference, but in fact there is a - very significant one: <varname>$bar1</varname> and - <varname>$globalref[0]</varname> are _NOT_ referenced, they - are NOT the same variable. This is because "new" does not - return a reference by default, instead it returns a copy. + ÏÔȻûÓÐÇø±ð£¬µ«Êµ¼ÊÉÏÓÐÒ»¸ö·Ç³£ÖØÒªµÄÇø±ð£º<varname>$bar1</varname> + ºÍ <varname>$globalref[0]</varname> + ²¢Ã»Óб»ÒýÓã¬ËüÃDz»ÊÇͬһ¸ö±äÁ¿¡£ÕâÊÇÒòΪ¡°new¡±Ä¬Èϲ¢²»·µ»ØÒýÓ㬶ø·µ»ØÒ»¸ö¿½±´¡£ <note> <simpara> - There is no performance loss (since PHP 4 and up use reference - counting) returning copies instead of references. On the - contrary it is most often better to simply work with copies - instead of references, because creating references takes some - time where creating copies virtually takes no time (unless none - of them is a large array or object and one of them gets changed - and the other(s) one(s) subsequently, then it would be wise to - use references to change them all concurrently). + ÔÚ·µ»Ø¿½±´¶ø²»ÊÇÒýÓÃÖв¢Ã»ÓÐÐÔÄÜÉϵÄËðʧ£¨ÒòΪ + PHP 4 ¼°ÒÔÉϰ汾ʹÓÃÁËÒýÓüÆÊý£©¡£Ïà·´¸ü¶àÇé¿öϹ¤×÷ÓÚ¿½±´¶ø²»ÊÇÒýÓÃÉϸüºÃ£¬ÒòΪ½¨Á¢ÒýÓÃÐèҪһЩʱ¼ä¶ø½¨Á¢¿½±´Êµ¼ÊÉϲ»»¨Ê±¼ä£¨³ý·ÇËüÃǶ¼²»ÊÇ´óµÄÊý×é»ò¶ÔÏ󣬶øÆäÖÐÖ®Ò»¸ú×ÅÁíÒ»¸ö±ä£¬ÄÇʹÓÃÒýÓÃÀ´Í¬Ê±ÐÞ¸ÄËüÃÇ»á¸ü´ÏÃ÷һЩ£©¡£ </simpara> </note> - To prove what is written above let us watch the code below. + ÒªÖ¤Ã÷ÒÔÉÏдµÄ£¬¿´¿´ÏÂÃæµÄ´úÂë¡£ <informalexample> <programlisting role="php"> <![CDATA[ -// now we will change the name. what do you expect? -// you could expect that both $bar1 and $globalref[0] change their names... +// ÏÖÔڸĸöÃû×Ö£¬ÄãÔ¤ÆÚʲô½á¹û£¿ +// Äã¿ÉÄÜÔ¤ÆÚ $bar1 ºÍ $globalref[0] ¶þÕßµÄÃû×Ö¶¼¸ÄÁË... $bar1->setName('set from outside'); -// as mentioned before this is not the case. +// µ«ÈçÍ¬Ç°ÃæËµµÄ£¬²¢²»ÊÇÕâÑù¡£ $bar1->echoName(); $globalref[0]->echoName(); -/* output: +/* Êä³öΪ£º set from outside set in constructor */ -// let us see what is different with $bar2 and $globalref[1] +// ÏÖÔÚ¿´¿´ $bar2 ºÍ $globalref[1] ÓÐûÓÐÇø±ð $bar2->setName('set from outside'); -// luckily they are not only equal, they are the same variable -// thus $bar2->name and $globalref[1]->name are the same too +// ÐÒÔ˵ÄÊÇËüÃDz»µ«Ïàͬ£¬¸ù±¾¾ÍÊÇͬһ¸ö±äÁ¿¡£ +// Òò´Ë $bar2->name ºÍ $globalref[1]->name Ò²ÊÇͬһ¸ö±äÁ¿¡£ $bar2->echoName(); $globalref[1]->echoName(); -/* output: +/* Êä³öΪ£º set from outside set from outside */ ]]> @@ -808,7 +757,7 @@ </informalexample> </para> <para> - Another final example, try to understand it. + ×îºó¸ø³öÁíÒ»¸öÀý×Ó£¬ÊÔ×ÅÀí½âËü¡£ <informalexample> <programlisting role="php"> @@ -818,7 +767,7 @@ function A($i) { $this->value = $i; - // try to figure out why we do not need a reference here + // ÊÔ×ÅÏëÃ÷°×ΪʲôÕâÀï²»ÐèÒªÒýÓà $this->b = new B($this); } @@ -847,8 +796,8 @@ } } -// try to undestand why using a simple copy here would yield -// in an undesired result in the *-marked line +// ÊÔ×ÅÀí½âΪʲôÕâÀïÒ»¸ö¼òµ¥µÄ¿½±´»áÔÚÏÂÃæÓà * +// ±ê³öÀ´µÄÐÐÖвúÉúÔ¤ÆÚÖ®ÍâµÄ½á¹û $a =& new A(10); $a->createRef(); @@ -863,7 +812,7 @@ $a->c->echoValue(); /* -output: +Êä³öΪ£º class A: 10 class B: 10 class B: 10