kk Fri May 18 05:44:37 2001 EDT Modified files: /phpdoc/en/language oop.xml Log: - Some clarifications. - Normalized spelling of product names (PHP 3 and PHP 4) to go with the rest of our docs.
Index: phpdoc/en/language/oop.xml diff -u phpdoc/en/language/oop.xml:1.13 phpdoc/en/language/oop.xml:1.14 --- phpdoc/en/language/oop.xml:1.13 Fri May 18 00:47:01 2001 +++ phpdoc/en/language/oop.xml Fri May 18 05:44:37 2001 @@ -43,7 +43,7 @@ <caution> <simpara> - The following cautionary note are valid for PHP4. + The following cautionary notes are valid for PHP 4. </simpara> <simpara> @@ -103,7 +103,7 @@ <para> Classes are types, that is, they are blueprints for actual variables. You have to create a variable of the desired type with - the new operator. + the <literal>new</literal> operator. </para> <informalexample> @@ -126,18 +126,20 @@ <para> Both, $cart and $another_cart, have functions add_item(), - remove_item() and a variable items. These are distinct functions and - variables. You can think of the objects as something like - directories in a filesystem, where you can have two different - files README.TXT, as long as they are in different directories, - and you'll have to type the full pathname in order to reach each - file from the toplevel directory. In PHP terms, the toplevel - directory would be the global namespace, and the pathname separator - would be ->. Thus, the names $cart->items and - $another_cart->items name two different variables. Note - that the variable is named $cart->items, not - $cart->$items, that is, a variable name in PHP has only a - single dollar sign. + remove_item() and a variable items. These are distinct + functions and variables. You can think of the objects as + something similar to directories in a filesystem. In a + filesystem you can have two different files README.TXT, as + long as they are in different directories. Just like with + directories where you'll have to type the full pathname in + order to reach each file from the toplevel directory, you + have to specify the complete name of the function you want + to call: In PHP terms, the toplevel directory would be the + global namespace, and the pathname separator would be ->. + Thus, the names $cart->items and $another_cart->items + name two different variables. Note that the variable is + named $cart->items, not $cart->$items, that is, a + variable name in PHP has only a single dollar sign. </para> <informalexample> @@ -156,11 +158,11 @@ </informalexample> <para> - Within a class, you do not know under which name the object will + Within a class definition, you do not know under which name the object will be accessible in your program: At the time the Cart class was written, it was unknown that the object will be named $cart or $another_cart later. Thus, you cannot write $cart->items within - the Cart class. Instead, in order to be able to access it's own + the Cart class itself. Instead, in order to be able to access it's own functions and variables from within a class, one can use the pseudo-variable $this which can be read as 'my own' or 'current object'. Thus, '$this->items[$artnr] += $num' can @@ -225,22 +227,24 @@ <caution> <simpara> - In PHP3 and PHP4 constructors behave differently. The PHP4 + In PHP 3 and PHP 4 constructors behave differently. The PHP 4 semantics are strongly preferred. </simpara> </caution> <para> Constructors are functions in a class that are automatically - called when you create a new instance of a class. In PHP3, a + called when you create a new instance of a class with + <literal>new</literal>. In PHP 3, a function becomes a constructor when it has the same name as - the class. In PHP4, a function becomes a constructor, when - it has the same name as the class it is defined in. + the class. In PHP 4, a function becomes a constructor, when + it has the same name as the class it is defined in - the + difference is subtle, but crucial (see below). </para> <informalexample> <programlisting role="php"> -// Works in PHP3 and PHP4. +// Works in PHP 3 and PHP 4. class Auto_Cart extends Cart { function Auto_Cart () { $this->add_item ("10", 1); @@ -253,7 +257,7 @@ This defines a class Auto_Cart that is a Cart plus a constructor which initializes the cart with one item of article number "10" each time a new Auto_Cart is being made with "new". Constructors - can also take arguments and these arguments can be optional, which + can take arguments and these arguments can be optional, which makes them much more useful. To be able to still use the class without parameters, all parameters to constructors should be made optional by providing default values. @@ -261,7 +265,7 @@ <informalexample> <programlisting role="php"> -// Works in PHP3 and PHP4. +// Works in PHP 3 and PHP 4. class Constructor_Cart extends Cart { function Constructor_Cart ($item = "10", $num = 1) { $this->add_item ($item, $num); @@ -280,7 +284,7 @@ <caution> <simpara> - In PHP3, derived classes and constructors have a number of + In PHP 3, derived classes and constructors have a number of limitations. The following examples should be read carefully to understand these limitations. </simpara> @@ -300,23 +304,23 @@ } } -// no constructor is being called in PHP3. +// no constructor is being called in PHP 3. $b = new B; </programlisting> </informalexample> <para> - In PHP3, no constructor is being called in the above example. - The rule in PHP3 is: 'A constructor is a function of the same + In PHP 3, no constructor is being called in the above example. + The rule in PHP 3 is: 'A constructor is a function of the same name as the class.'. The name of the class is B, and there is no function called B() in class B. Nothing happens. </para> <para> - This is fixed in PHP4 by introducing another rule: If a class + This is fixed in PHP 4 by introducing another rule: If a class has no constructor, the constructor of the base class is being called, if it exists. The above example would have printed - 'I am the constructor of A.<br>' in PHP4. + 'I am the constructor of A.<br>' in PHP 4. </para> <informalexample> @@ -344,24 +348,24 @@ </informalexample> <para> - In PHP3, the function B() in class A will suddenly become a + In PHP 3, the function B() in class A will suddenly become a constructor in class B, although it was never intended to be. - The rule in PHP3 is: 'A constructor is a function of the same - name as the class.'. PHP3 does not care if the function is + The rule in PHP 3 is: 'A constructor is a function of the same + name as the class.'. PHP 3 does not care if the function is being defined in class B, or if it has been inherited. </para> <para> - This is fixed in PHP4 by modifying the rule to: 'A constructor + This is fixed in PHP 4 by modifying the rule to: 'A constructor is a function of the same name as the class it is being defined - in.'. Thus in PHP4, the class B would have no constructor function + in.'. Thus in PHP 4, the class B would have no constructor function of its own and the constructor of the base class would have been called, printing 'I am the constructor of A.<br>'. </para> <caution> <simpara> - Neither PHP3 nor PHP4 call constructors of the base class + Neither PHP 3 nor PHP 4 call constructors of the base class automatically from a constructor of a derived class. It is your responsibility to propagate the call to constructors upstream where appropriate. @@ -370,7 +374,7 @@ <note> <simpara> - There are no destructors in PHP3 or PHP4. You may use + There are no destructors in PHP 3 or PHP 4. You may use <function>register_shutdown_function</function> instead to simulate most effects of destructors. </simpara> @@ -389,7 +393,7 @@ <caution> <simpara> - The following is valid for PHP4 only. + The following is valid for PHP 4 only. </simpara> </caution> @@ -452,7 +456,9 @@ 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. + ::-operator. Write A::example() to do this (in fact, you + should be writing parent::example(), as shown in the next + section). </para> <para> @@ -513,7 +519,7 @@ <note> <simpara> - In PHP3, objects will lose their class association + 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 @@ -523,7 +529,7 @@ <caution> <simpara> - The following information is valid for PHP4 only. + The following information is valid for PHP 4 only. </simpara> </caution>