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.
Note:
Parent constructors are not called implicitly if the child class defines
a constructor. In order to run a parent constructor, a call to
parent::__construct() within the child constructor is
required.
Example 19-6. using new unified constructors
<?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(); ?>
For backwards compatibility, if PHP 5 cannot find a
__construct() 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
__construct() which was used for different semantics.
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.
Example 19-7. Destructor Example
<?php class MyDestructableClass { function __construct() { print "In constructor\n"; $this->name = "MyDestructableClass"; }
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 parent::__destruct() in the destructor
body.
Note:
Destructor is called during the script shutdown so headers are always
already sent.
Php language.oop5.decon Function syntax tag
language.oop5.decon php code on this is provided for your study purpose, it will guide you to know how create and design a website using php. use it to practice and train your self online
Php language.oop5.decon syntax tutorial
php tutorial guide and code design are for easy learning and programming. The code practice section provided at the top is for practising of this syntax. Use the code section up to practice your php programming online. Learning php is very easy, all you need is to use the examples on this site and practice them to perfect your skills.