vrana Wed Apr 6 05:10:50 2005 EDT
Modified files:
/phpdoc/en/language oop.xml
Log:
Constructors - remove PHP 3 stuff, fix description of constructors inherited
from parent class (bug #22253)
http://cvs.php.net/diff.php/phpdoc/en/language/oop.xml?r1=1.61&r2=1.62&ty=u
Index: phpdoc/en/language/oop.xml
diff -u phpdoc/en/language/oop.xml:1.61 phpdoc/en/language/oop.xml:1.62
--- phpdoc/en/language/oop.xml:1.61 Wed Mar 9 06:40:59 2005
+++ phpdoc/en/language/oop.xml Wed Apr 6 05:10:49 2005
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
-<!-- $Revision: 1.61 $ -->
+<!-- $Revision: 1.62 $ -->
<chapter id="language.oop">
<title>Classes and Objects (PHP 4)</title>
@@ -389,28 +389,19 @@
<sect1 id="language.oop.constructor">
<title>Constructors</title>
- <caution>
- <simpara>
- 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 with
- <literal>new</literal>. In PHP 3, a
- function becomes a constructor when it has the same name as
- 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).
+ <literal>new</literal>. A function becomes a constructor, when
+ it has the same name as the class. If a class
+ has no constructor, the constructor of the base class is being
+ called, if it exists.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
-// Works in PHP 3 and PHP 4.
class Auto_Cart extends Cart {
function Auto_Cart() {
$this->add_item("10", 1);
@@ -435,7 +426,6 @@
<programlisting role="php">
<![CDATA[
<?php
-// Works in PHP 3 and PHP 4.
class Constructor_Cart extends Cart {
function Constructor_Cart($item = "10", $num = 1) {
$this->add_item ($item, $num);
@@ -443,11 +433,9 @@
}
// Shop the same old boring stuff.
-
$default_cart = new Constructor_Cart;
// Shop for real...
-
$different_cart = new Constructor_Cart("20", 17);
?>
]]>
@@ -460,51 +448,6 @@
<literal>@new</literal>.
</para>
- <caution>
- <simpara>
- In PHP 3, derived classes and constructors have a number of
- limitations. The following examples should be read carefully
- to understand these limitations.
- </simpara>
- </caution>
-
- <informalexample>
- <programlisting role="php">
-<![CDATA[
-<?php
-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>
- 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 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 PHP 4.
- </para>
-
<informalexample>
<programlisting role="php">
<![CDATA[
@@ -525,13 +468,9 @@
class B extends A
{
- function C()
- {
- echo "I am a regular function.<br />\n";
- }
}
-// This will call B() as a constructor.
+// This will call B() as a constructor
$b = new B;
?>
]]>
@@ -539,43 +478,27 @@
</informalexample>
<para>
- In PHP 3, the function B() in class A will suddenly become a
+ The function B() in class A will suddenly become a
constructor in class B, although it was never intended to be.
- 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
+ PHP 4 does not care if the function is
being defined in class B, or if it has been inherited.
</para>
- <para>
- 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 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 PHP 3 nor PHP 4 call constructors of the base class
+ PHP 4 doesn't 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.
</simpara>
</caution>
- <note>
- <simpara>
- 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>
- </note>
-
<para>
Destructors are functions that are called automatically
when an object is destroyed, either with <function>unset</function>
or by simply going out of scope. There are no destructors
- in PHP.
+ in PHP. You may use <function>register_shutdown_function</function>
+ instead to simulate most effects of destructors.
</para>
</sect1>