cvs: phpdoc /he/language oop.xml

From: Date: Sat, 26 Jan 2002 00:54:53 +0000
Subject: cvs: phpdoc /he/language oop.xml
Groups: php.doc 
Request: Send a blank email to phpdoc+get-969341622@lists.php.net to get a copy of this message
barak Fri Jan 25 19:54:53 2002 EDT Added files: /phpdoc/he/language oop.xml Log: initial of translating this file

Index: phpdoc/he/language/oop.xml +++ phpdoc/he/language/oop.xml <?xml version="1.0" encoding="iso-8859-1"?> <!-- $Revision: 1.1 $ --> <chapter id="language.oop"> <title>Classes and Objects</title> <sect1 id="keyword.class"> <title><literal>îçì÷åú</literal></title> <para> îçì÷ä äéà àåñó ùì îùúðéí åôåð÷öéåú ùòåáãåú òí îùúðéí àìå. îçì÷ä îåâãøú úåê ùéîåù áúçáéø äáà: <informalexample> <programlisting role="php"> <![CDATA[ <?php class Cart { var $items; // äâãøú îùúðä ùì äîçì÷ä áùí $items // Add $num articles of $artnr to the cart function add_item ($artnr, $num) { $this->items[$artnr] += $num; } // Take $num articles of $artnr out of the cart function remove_item ($artnr, $num) { if ($this->items[$artnr] > $num) { $this->items[$artnr] -= $num; return true; } else { return false; } } } ?> ]]> </programlisting> </informalexample> </para> <para> áãåâîà äð"ì, äåâãøä îçì÷ä ùùîä Cart, ùîåøëáú îîòøê äîùåééê ìôøéèéí ùáúåê áñì (Cart), åîùðé ôåð÷öéåú ìäåñôä (add) åìäñøä (remove) ùì ôøéèéí îúåê äòâìä. </para> <caution> <simpara> äîãøéê äáà îúàéí ì PHP4 !. </simpara> <simpara> äîéìä <literal>stdClass</literal> äéà îéìä ùîåøä äðîöàú áùéîåù ôðéîé òì éãé ZEND. ìëï, àéï ìäùúîù áîéìä <literal>stdClass</literal> á PHP. </simpara> <simpara> ùí äôåð÷öéä <literal>__sleep</literal> åùí äôåð÷öéä <literal>__wakeup</literal> áòìåú ùéîåù îéåçã á - php. àúá ìà éëåì ìäùúîù áùí äæää ìàçú îùúé äôåð÷öéåú äð"ì áîçì÷ä ëìùäé àìà àí àúä øåöä ìäùúîù áäï ìàôùøåú äîéåçãú (ùúôåøè áäîùê) ùìä äï ðåòãå. øàä áäîùê äîãøéê ìîéãò ðåñó. </simpara> <simpara> ëì ôåð÷öéåú ä PHP äîåôìàåú (áäîùê éôåøè îéãò ðåñó áðåùà) îúçéìåú òí __. îåîìõ îàåã ìà ì÷øåà ìùîåú ôåð÷öéä á PHP òí ùí äîúçéì á __ àìà àí àúä øåöä ìúòã àåúï ëôåð÷öéåú "÷ñåîåú". </simpara> </caution> <note> <simpara> á PHP 4, àéúçåì îùúðé îçì÷ä ùäåâãøå áòæøú <literal>var</literal> àôùøé ø÷ òí ÷áåòéí. ëãé ìàúçì îùúðéí, ùìà òì éãé òøëé ÷áåòéí, àúä öøéê ìäùúîù áôåð÷öéä ùð÷øàú àåèåîèéú òì éãé PHP ëàùø ðåöø àåáéé÷è çãù ùì äôåð÷öéä. ôåð÷öéä ëæàú ð÷øàú "ôåð÷öéä áðàéú" (constructor) (øàä ìîèä). </simpara> <informalexample> <programlisting role="php"> <![CDATA[ <?php /* áîçì÷ä äáàä, úâøåø äåãòú ùâéàä áâìì àéúçåì îùúðéí ùâåé ! */ class Cart { var $todays_date = date("Y-m-d"); var $name = $firstname; var $owner = 'Fred ' . 'Jones'; var $items = array("VCR", "TV"); } /* ëëä æä àîåø ìäòùåú: */ class Cart { var $todays_date; var $name; var $owner; var $items; function Cart() // äáðàé ùì äîçì÷ä { $this->todays_date = date("Y-m-d"); $this->name = $GLOBALS['firstname']; /* etc. . . */ } } ]]> </programlisting> </informalexample> </note> <para> îçì÷åú äï úáðéåú, ëìåîø, äï äëðä ìéöéøú îùúðéí àîéúééí (äàåáéé÷èéí) éåöøéí àú äîùúðéí îäñåâ äîáå÷ù úåê ùéîåù áàåôøèåø <literal>new</literal>. </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php $cart = new Cart; $cart->add_item("10", 1); $another_cart = new Cart; $another_cart->add_item("0815", 3); ]]> </programlisting> </informalexample> <para> äãåâîà äð"ì éöøä àú äàåáéé÷èéí $cart å $another_cart, ùðéäí àåáéé÷èéí ùðåöøå îäîçì÷ä Cart. äôåð÷öéä add_item() ùì äàåáéé÷è - $cart ð÷øàú òì îðú ìäåñéó ôøéè àçã ùì çôõ îñ' 10 ìúåê äòâìä. 3 ôøéèéí çôõ îñ' 0815 ðåñôå ìàåáéé÷è $another_cart. </para> <para> âí ì $cart åâí ì $another_cart, éù àú äôåð÷öéåú add_item(), remove_item() åîùúðä ùì ôøéèéí ($items) . àìå äí ôåð÷öéåú åîùúðéí ðôøãéí. àôùø ìçùåá òì àåáéé÷èéí ëãåîéí ìñôøéåú å÷áöéí áîòøëú ÷áöéí. áúåê îòøëú ÷áöéí éëåìéí ìäéåú ìê ùðé ÷áöé README.TXT ùåðéí ìçìåèéï, ëì òåã äí ééîöàå áñôøéä àçøú. îîù ëîå áîòøëåú ÷áöéí, ëùöøéê ìäì÷éã àú äîñìåì äîìà òì îðú ìäôòéì ÷åáõ ëìùäå, àí ìà ðîöàéí áñôøéä ùìå, éù öåøê ìúú àú äùí äîìà ùì äôåð÷öéä àåúä øåöéí ìäôéì, áîåðçéí ùì php: "øîú äâåáä" ùì äàåáéé÷è ð÷áò áòæøú ùîåú ùäåâãøå, åîäôøéã áéï ùîåú àìå -&gt;. ìëï, äîéìéí $cart-&gt;items å $another_cart-&gt;items äï ùîåú ùì ùúé îùúðéí àçøéí. éù ìùéí ìá ìòåáãä ùäîùúðä ð÷øà $cart-&gt;items, åìà $cart-&gt;$items, åæàú áùì, ùìùí îùúðä á PHP éù ø÷ ñéîï $ àçã. </para> <informalexample> <programlisting role="php"> <![CDATA[ // correct, single $ $cart->items = array("10" => 1); // ùâåé, áâìì ù $cart->$items äåôê ìäéåú $cart->"" $cart->$items = array("10" => 1); //ðëåï, àáì òìåì ìäéåú âí îùäå ùìà àìéå äúëååðå //$cart->$myvar äôê ìäéåú $cart->items $myvar = 'items'; $cart->$myvar = array("10" => 1); ]]> </programlisting> </informalexample> <para> òí äâãøú äîçì÷ä, àéï àôùøåú ìãòú àéìå ùîåú àåáéé÷èéí ééåöøå áúåëðéú ìàåúä îçì÷ä: áæîï ùäîçì÷ä Cart ðëúáä, ìà äéä éãåò ùééåöø äàåáéé÷è $cart àå äàåáéé÷è $another_cart îàåçø éåúø. ìëï, áìúé àôùøé ìëúåá $cart-&gt;items áúåê äîçì÷ä Cart òöîä. áî÷åí æàú, ëãé ùàôùø éäéä ìôðåú áúåê ìúåê äôåð÷öéåú åäîùúðéí ùì äàåéé÷è áúåê äîçì÷ä òöîä, àôùø ìäùúîù á áôñàåãå - îùúðä (îùúðä îãåîä) $this ùéëåì ìäçùá ë 'ùì òöîé' àå 'àåáéé÷è ðåëçé'. ìôéëê, '$this-&gt;items[$artnr] += $num' éëåì ìä÷øà ë 'äåñó $num ìîåðä $artnr ùì îòøì äçôöéí ùì òöîé' àå 'äåñó $num ìîåðä $artnr ùì îòøê äçôöéí ùðîöà áúåê äàåáéé÷è äðåëçé'. </para> </sect1> <sect1 id="keyword.extends"> <title><literal>äåøùåú</literal></title> <para> ìòééí ÷øåáåú, æ÷å÷éí ìîçì÷åú òí òí îùúðéí åôåð÷öéåú ãåîåú ìîùúðéí åîçì÷åú áôåð÷öéåú àçøåú. ìîòùä, æä ðåäâ èåá ìäâãéø îçì÷ä ëììéú ùéëåìä ìùîù àåúê áëì äôøåéé÷èéí ùìê åìäúàéí àú îçì÷ä æàú ìöøëé ëì àçã îäôøåéé÷èéí ñôöéôú. ìñééò áæàú, îçì÷åú éëåìåú ìäéåú äøçáåú ùì îçì÷åú àçøåú. ìîçì÷ä ùîøçéáä àå äéåøùú éù àú ëì äîùúðéí åäôåð÷öéåú ùì äîçì÷ä äáñéñéú (æä ð÷øà 'éøåùä' ìîøåú äòåáãä ùàó àçã ìà îú) åëï àú îä ùàúä îåñéó ìä (ôåð÷öéåú åîùúðéí ðåñôéí) æä áìúé àôùøé ìáèì äâãøä ùì îùúðä àå ôåð÷öéä ëìùäé áîçì÷ä. îçì÷ä îøçéáä, ðùòðú ìòåìí òì áñéñ ùì îçì÷ä àçú áìáã,åæàú áâìì, ùäåøùä îøåáä ìà ðúîëú. îçì÷åú îåøçáåú úåê ùéîåù áîéìä äùîåøä 'extends'. </para> <informalexample> <programlisting role="php"> <![CDATA[ class Named_Cart extends Cart { var $owner; function set_owner ($name) { $this->owner = $name; } } ]]> </programlisting> </informalexample> <para> áãåâîà äð"ì äåâãøä îçì÷ä Named_Cart ùéù ìä àú ëì äôåð÷öéåú åäîùúðéí ùì Cart áðåñó ìîùúðä $owner åìôðå÷öéä ðåñôú áùí set_owner(). éåöøéí àú äòãìä áãøê äøâéìä, àìà ùàôø âí ì÷áåò òøëéí åì÷øåà àú äòøëéí ùì áòì äòâìä. òãééï àôùø ìäùúîù ôåð÷öéåú äøâéìåú ùì äòâìä. </para> <informalexample> <programlisting role="php"> <![CDATA[ $ncart = new Named_Cart; // éöéøú àåáéé÷è $ncart->set_owner("kris"); // äëðñú ùí ùì áòìéí ìàåúä òâìä print $ncart->owner; // äöâä ùì òøê äáòìéí ùì äòâìä $ncart->add_item("10", 1); // ùéîåù áôåð÷öéä ùäåøùðå îúåê cart ]]> </programlisting> </informalexample> </sect1> <sect1 id="language.oop.constructor"> <title><literal>Constructors</literal></title> <caution> <simpara> á PHP 3 å PHP 4 ìôåð÷öéåú äáðàéåú îúðäâåú áöåøä ùåðä. äñîðèé÷ä ùì PHP 4 òãéôä áäøáä. </simpara> </caution> <para> áðàéí äí ôåð÷öéåú áîçì÷ä ùîåôòìéí àåèåîèéú ëùéåöøéí îåôò çãù ùì äîçì÷ä (áòú éöéøú àåáéé÷è çãù) úåê ùéîåù áîéìä <literal>new</literal>. á PHP 3, ôåð÷öéåú îåâãøåú ëáðàéåú ëùäï ð÷øàåú áùí äæää ìùí äîçì÷ä. á PHP 4, ôåð÷öéåú îùîùåú ëáðàéåú, ëùäï áòìåú àåúå ùí ùì îçì÷ä ùäï îåâãøåú áúåëï - ääáãì äåà ìëàåøä ÷èï, àáì ÷øéèé (øàä ìîèä). </para> <informalexample> <programlisting role="php"> <![CDATA[ // òåáã á PHP 3 åâí á PHP 4. class Auto_Cart extends Cart { function Auto_Cart() { $this->add_item ("10", 1); } } ]]> </programlisting> </informalexample> <para> áåãâîà äåâãøä îçì÷ä Auto_Cart ùäéà Cart åáðåñó ìä áðàé ùîàúçì àú äòâìä òí øéè àçã ùì îåöø ùîñôøå "10" áëì ôòí ùéåöøéí îåôò çãù ùì Auto_Cart áòæøú "new". äôð÷öéä äáðàéú éëåìä ì÷çú àøâåîðèéí ùäí àåôöéåðìééí, îä ùâåøí ìäí ìäéåú ùéîåùééí áîéåçã. òì îðú ùàôùø éäéä ìäùúîù áôåð÷öéä áìé ôøîèøéí, ëì äôøîèøéí ùîåòáøéí ìáðàé öøéëí ìä÷áò àåôöéåðìéú òì éãé àñô÷ú òøëé áøéøú îçãì. </para> <informalexample> <programlisting role="php"> <![CDATA[ // òåáã âí á PHP 3 åâí á PHP 4. class Constructor_Cart extends Cart { function Constructor_Cart($item = "10", $num = 1) { $this->add_item ($item, $num); } } // Shop the same old boring stuff. $default_cart = new Constructor_Cart; // Shop for real... $different_cart = new Constructor_Cart("20", 17); ]]> </programlisting> </informalexample> <caution> <simpara> á PHP 3, ìîçì÷åú åáðàéí ðâæøéí éù ëîä îâáìåú. äãåâîà äáàä öøéëä ìä÷øà áæäéøåú òì îðú ùääâáìåú éäéå îåáðåú. </simpara> </caution> <informalexample> <programlisting role="php"> <![CDATA[ class A { function A() { echo "I am the constructor of A.<br>\n"; } } class B extends A { function C() { echo "I am a regular function.<br>\n"; } } // no constructor is being called in PHP 3. $b = new B; ]]> </programlisting> </informalexample> <para> á PHP 3, ùåí ôåð÷öéä áðàéú ìà ð÷øà áãåâîà äð"ì. äçå÷ á PHP 3 äåà: 'äáðàé A äåà äôåð÷öéä ùì àåúå ùí ùì äîçì÷ä.'. äùí ùì äîçì÷ä äåà B, åàéï ùåí ôåð÷öéä ùð÷øàú B() áîçì÷ä B. ìëï ëìåí ìà ÷åøä. </para> <para> òðééï æäá úå÷ï á PHP 4 òì éãé äëììú çå÷ çãù: àí ìîçì÷ä àéï ôåð÷öéä áðàéú, äôåð÷öéä áðàéú ùì îçì÷ú äáñéñ ð÷øàú, àí äéà ÷ééîú. áãåâîà îòì, á PHP4 áòú éöéøú äàåééá÷è éåãôñ äôìè: 'I am the constructor of A.&lt;br>'. </para> <informalexample> <programlisting role="php"> <![CDATA[ class A { function A() { echo "I am the constructor of A.<br>\n"; } function B() { echo "I am a regular function named B in class A.<br>\n"; echo "I am not a constructor in A.<br>\n"; } } class B extends A { function C() { echo "I am a regular function.<br>\n"; } } // This will call B() as a constructor. $b = new B; ]]> </programlisting> </informalexample> <para> á PHP 3, äôåð÷öéä B() ùáîçì÷ä A ìôúò úäôåê ìáðàéú áîçì÷ä B, ìîøåú ùìà ìæä "äúëååï äîùåøø". äçå÷ á PHP 3 äåà: 'äáðàé A äéà äôåð÷öéä áòìú àåúå ùí ùì äîçì÷ä.'.ì PHP 3 ìà àéëôú àí äôåð÷öéä äåâãøä áîçì÷ä B, àå äí äéà ðâæøä îîçì÷ä A . </para> <para> âí òðééï æä úå÷ï á PHP 4 òì éãé éöéøú äçå÷ äáà: 'äôåð÷öéä äðáàéú A äéà ôåð÷öéä áòìú àåú ùí ùì äîçì÷ä ùäéà îåâãøú áúåëä. 'ìëï á PHP 4, äîçì÷ä B ùàéï ìä àú äáðàéú ùì òöîä úùúîù áôåð÷öéä äáðàéú ùì îçì÷ú äáñéñ, åìôéëê éåãôñ òì äîñê 'I am the constructor of A.&lt;br>'. </para> <caution> <simpara> ìà PHP 3 åìà PHP 4 ÷åøàéí ìáðàé ùì îçì÷ú äáñéñ àåèåîèéú îáðàé ùì îçì÷ä ðâæøú. æä áàçøéåú äîùúîù ì÷åøà ìôåð÷öéä äáðàéú ùì îçì÷ú äáñéñ áî÷øä ëæä. </simpara> </caution> <note> <simpara> àéï "ôåð÷öéä äåøñú" (destructors) á PHP 3 àå PHP 4. àúä éëåì ìäùúîù á <function>register_shutdown_function</function> áî÷åí æàú ìãîåú àú øåá äàñô÷èéí ùì "ôåð÷öéä äåøñú" (ääôéê îáðàéú). </simpara> </note> <para> "ôåð÷öéä äåøñú" äéà ôåð÷öéä ùð÷øàú àåèîèéú ëàùø "îùîéãéí" àåáéé÷è, àí òì éãé ùéîåù á <function>unset</function> åàí ôùåè òì éãé éöéàä îçåõ ìúçåí ùì äàåáéé÷è. ëîå ùðàîø, àéï ôåð÷öéåú îñåâ æä á PHP. </para> </sect1> <sect1 id="keyword.paamayim-nekudotayim"><!-- :-) --> <title><literal>::</literal></title> <caution> <simpara> ä÷èò äáà ú÷ó ì PHP 4 áìáã. </simpara> </caution> <para> ìòéúéí æä ùéîåùé ìäúééçñ ì functions and variables in base classes or to refer to functions in classes that have not yet any instances. The :: operator is being used for this. </para> <informalexample> <programlisting role="php"> <![CDATA[ class A { function example() { echo "I am the original function A::example().<br>\n"; } } class B extends A { function example() { echo "I am the redefined function B::example().<br>\n"; A::example(); } } // there is no object of class A. // this will print // I am the original function A::example().<br> A::example(); // create an object of class 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(); ]]> </programlisting> </informalexample> <para> The above example calls the function example() in class A, but there is no object of class A, so that we cannot write $a->example() or similar. Instead we call example() as a 'class function', that is, as a function of the class itself, not any object of that class. </para> <para> There are class functions, but there are no class variables. In fact, there is no object at all at the time of the call. Thus, a class function may not use any object variables (but it can use local and global variables), and it may no use $this at all. </para> <para> In the above example, class B redefines the function example(). The original definition in class A is shadowed and no longer available, unless you are refering specifically to the implementation of example() in class A using the ::-operator. Write A::example() to do this (in fact, you should be writing parent::example(), as shown in the next section). </para> <para> In this context, there is a current object and it may have object variables. Thus, when used from WITHIN an object function, you may use $this and object variables. </para> </sect1> <sect1 id="keyword.parent"> <title><literal>parent</literal></title> <para> You may find yourself writing code that refers to variables and functions in base classes. This is particularly true if your derived class is a refinement or specialisation of code in your base class. </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> declation 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. </para> <informalexample> <programlisting role="php"> <![CDATA[ class A { function example() { echo "I am A::example() and provide basic functionality.<br>\n"; } } class B extends A { function example() { echo "I am B::example() and provide additional functionality.<br>\n"; parent::example(); } } $b = new B; // This will call B::example(), which will in turn call A::example(). $b->example(); ]]> </programlisting> </informalexample> </sect1> <sect1 id="language.oop.serialization"> <title>Serializing objects - objects in sessions</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). </simpara> </note> <caution> <simpara> The following information is valid for PHP 4 only. </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. </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. </para> <informalexample> <programlisting role="php"> <![CDATA[ classa.inc: class A { var $one = 1; function show_one() { echo $this->one; } } page1.php: include("classa.inc"); $a = new A; $s = serialize($a); // store $s somewhere where page2.php can find it. $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(); ]]> </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. </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. </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. </para> </sect1> <sect1 id="language.oop.magic-functions"> <title>The magic functions <literal>__sleep</literal> and <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. </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. </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. </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. </para> </sect1> <sect1 id="language.oop.newref"> <title>References inside the constructor</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"> <![CDATA[ class Foo { function Foo($name) { // create a reference inside the global array $globalref global $globalref; $globalref[] = &$this; // set name to passed value $this->setName($name); // and put it out $this->echoName(); } function echoName() { echo "<br>",$this->name; } function setName($name) { $this->name = $name; } } ]]> </programlisting> </informalexample> </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>=&amp;</literal> operator... <informalexample> <programlisting role="php"> <![CDATA[ $bar1 = new Foo('set in constructor'); $bar1->echoName(); $globalref[0]->echoName(); /* output: set in constructor set in constructor set in constructor */ $bar2 =& new Foo('set in constructor'); $bar2->echoName(); $globalref[1]->echoName(); /* output: set in constructor set in constructor set in constructor */ ]]> </programlisting> </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. <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). </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->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->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 $bar2->echoName(); $globalref[1]->echoName(); /* output: set from outside set from outside */ ]]> </programlisting> </informalexample> </para> <para> Another final example, try to understand it. <informalexample> <programlisting role="php"> <![CDATA[ class A { function A($i) { $this->value = $i; // try to figure out why we do not need a reference here $this->b = new B($this); } function createRef() { $this->c = new B($this); } function echoValue() { echo "<br>","class ",get_class($this),': ',$this->value; } } class B { function B(&$a) { $this->a = &$a; } function echoValue() { echo "<br>","class ",get_class($this),': ',$this->a->value; } } // 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(); $a->echoValue(); $a->b->echoValue(); $a->c->echoValue(); $a->value = 11; $a->echoValue(); $a->b->echoValue(); // * $a->c->echoValue(); /* output: class A: 10 class B: 10 class B: 10 class A: 11 class B: 11 class B: 11 */ ]]> </programlisting> </informalexample> </para> </sect1> </chapter> <!-- 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 (#969341622) next »