php-doc-en/language/oop5/decon.xml
2005-03-23 10:34:41 +00:00

129 lines
3.6 KiB
XML

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.9 $ -->
<sect1 id="language.oop5.decon">
<title>Constructors and Destructors</title>
<sect2 id="language.oop5.decon.constructor">
<title>Constructor</title>
<methodsynopsis>
<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.
</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";
}
}
$obj = new BaseClass();
$obj = new SubClass();
?>
]]>
</programlisting>
</example>
<para>
For backwards compatibility, if PHP 5 cannot find a
<function>__construct</function> function for a given 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
<function>__construct</function> which was used for different semantics.
</para>
</sect2>
<sect2 id="language.oop5.decon.destructor">
<title>Destructor</title>
<methodsynopsis>
<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 all references to a particular object are removed or when
the object is explicitly destroyed.
</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.
</para>
<note>
<para>
Destructor is called during the script shutdown so headers are always
already sent.
</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:"../../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
-->