1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 329168 $ -->
<sect1 xml:id="language.oop5.decon" xmlns="http://docbook.org/ns/docbook">
<title>Constructors and Destructors</title>
<sect2 xml:id="language.oop5.decon.constructor">
<title>Constructor</title>
<methodsynopsis xml:id="object.construct">
<type>void</type><methodname>__construct</methodname>
<methodparam choice="opt"><type>mixed</type><parameter>args</parameter></methodparam>
<methodparam choice="opt"><parameter>...</parameter></methodparam>
</methodsynopsis>
<para>
PHP 5 allows developers to declare constructor methods for classes.
Classes which have a constructor method call this method on each
newly-created object, so it is suitable for any initialization that the
object may need before it is used.
</para>
<note>
<simpara>
Parent constructors are not called implicitly if the child class defines
a constructor. In order to run a parent constructor, a call to
<function>parent::__construct</function> within the child constructor is
required. If the child does not define a constructor then it may be inherited
from the parent class just like a normal class method (if it was not declared
as private).
</simpara>
</note>
<example>
<title>using new unified constructors</title>
<programlisting role="php">
<![CDATA[
<?php
class BaseClass {
function __construct() {
print "In BaseClass constructor\n";
}
}
class SubClass extends BaseClass {
function __construct() {
parent::__construct();
print "In SubClass constructor\n";
}
}
class OtherSubClass extends BaseClass {
// inherits BaseClass's constructor
}
// In BaseClass constructor
$obj = new BaseClass();
// In BaseClass constructor
// In SubClass constructor
$obj = new SubClass();
// In BaseClass constructor
$obj = new OtherSubClass();
?>
]]>
</programlisting>
</example>
<para>
For backwards compatibility, if PHP 5 cannot find a
<link linkend="object.construct">__construct()</link> function for a given class, and the
class did not inherit one from a parent class, it will
search for the old-style constructor function, by the name of the class.
Effectively, it means that the only case that would have compatibility
issues is if the class had a method named
<link linkend="object.construct">__construct()</link> which was used for different semantics.
</para>
<para>
Unlike with other methods, PHP will not generate an
<constant>E_STRICT</constant> level error message when
<link linkend="object.construct">__construct()</link> is overridden with different parameters
than the parent <link linkend="object.construct">__construct()</link> method has.
</para>
<para>
As of PHP 5.3.3, methods with the same name as the last element of a
namespaced class name will no longer be treated as constructor. This
change doesn't affect non-namespaced classes.
</para>
<example>
<title>Constructors in namespaced classes</title>
<programlisting role="php">
<![CDATA[
<?php
namespace Foo;
class Bar {
public function Bar() {
// treated as constructor in PHP 5.3.0-5.3.2
// treated as regular method as of PHP 5.3.3
}
}
?>
]]>
</programlisting>
</example>
</sect2>
<sect2 xml:id="language.oop5.decon.destructor">
<title>Destructor</title>
<methodsynopsis xml:id="object.destruct">
<type>void</type><methodname>__destruct</methodname>
<void />
</methodsynopsis>
<para>
PHP 5 introduces a destructor concept similar to that of other
object-oriented languages, such as C++. The destructor method will be
called as soon as there are no other references to a particular object,
or in any order during the shutdown sequence.
</para>
<example>
<title>Destructor Example</title>
<programlisting role="php">
<![CDATA[
<?php
class MyDestructableClass {
function __construct() {
print "In constructor\n";
$this->name = "MyDestructableClass";
}
function __destruct() {
print "Destroying " . $this->name . "\n";
}
}
$obj = new MyDestructableClass();
?>
]]>
</programlisting>
</example>
<para>
Like constructors, parent destructors will not be called implicitly by
the engine. In order to run a parent destructor, one would have to
explicitly call <function>parent::__destruct</function> in the destructor
body. Also like constructors, a child class may inherit the parent's
destructor if it does not implement one itself.
</para>
<para>
The destructor will be called even if script execution is stopped using
<function>exit</function>. Calling <function>exit</function> in a destructor
will prevent the remaining shutdown routines from executing.
</para>
<note>
<para>
Destructors called during the script shutdown have HTTP headers already
sent. The working directory in the script shutdown phase can be different
with some SAPIs (e.g. Apache).
</para>
</note>
<note>
<para>
Attempting to throw an exception from a destructor (called in the time of
script termination) causes a fatal error.
</para>
</note>
</sect2>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/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
-->
|