Constructors ¶
Void__construct ([Mixed$args [, $ ...]])
PHP 5 allows the developer to define a method as a constructor in a class. Classes with constructors Call this method each time a new object is created, making it ideal for doing some initialization work before using the object.
Note: If a constructor is defined in a subclass, the constructor of its parent class is not implicitly called. To execute the constructor of the parent class, you need to call Parent::__construct () in the constructor of the child class. If a subclass does not have a constructor defined, it inherits from the parent class like a normal class method (if it is not defined as private).
Example #1 using the new standard constructor
Class Baseclass{function __construct () {print "in BaseClass constructor<br>";}} Class Subclass extends Baseclass{function __construct () {parent::__construct ();p rint "in subclass CONSTRUCTOR<BR > ";}} Class Othersubclass extends baseclass{} $obj = new BaseClass (); $obj = new Subclass (); $obj = new Othersubclass ();
Output Result:
In BaseClass constructor
In BaseClass constructor
In subclass constructor
In BaseClass constructor
For backwards compatibility, if PHP 5 cannot find the __construct () function in the class and does not inherit one from the parent class, it will attempt to find the old constructor, which is the function with the same name as the class. So the only thing that can create a compatibility problem is when a method named __construct () already in the class is used for other purposes.
Unlike other methods, PHP does not produce a e_strict error message when __construct () is overwritten with a method that has different parameters from the parent class __construct ().
From PHP 5.3.3, in a namespace, a method with the same name as the class name is no longer a constructor. This change does not affect classes that are not in the namespace.
Example #2 Constructors in namespaced classes
Namespace Foo;class bar{public function Bar () {//is considered a constructor in php5.3.0-5.3.2//is considered a normal method since PHP5.3.3}}
destructor ¶
Void__destruct (void)
PHP 5 introduces the concept of destructors, similar to other object-oriented languages such as C + +. Destructors are removed when all references to an object are deleted or executed when an object is explicitly destroyed.
Example #3 destructor Example
<?phpclass mydestructableclass{function __construct () {print "in constructor<br>"; $this->name = " Mydestructableclass ";} function __destruct () {print "destroying". $this->name. ' <br> ';}} $obj = new Mydestructableclass ();? >
Output Result:
In constructor
Destroying Mydestructableclass
As with constructors, the destructor of the parent class is not called by the engine. To execute a destructor for a parent class, you must explicitly call Parent::__destruct () in the destructor body of the subclass. Also, as with constructors, subclasses inherit the parent class if they do not have a destructor defined by themselves.
Destructors are called even when the script is run with exit (). Calling exit () in the destructor will abort the operation of the rest of the shutdown operation.
Note:
The destructor is called when the script is closed, and all HTTP header information is sent. The working directory when the script is closed is likely to be different from the SAPI (such as Apache).
Note:
Attempting to throw an exception in a destructor (called when the script terminates) can result in a fatal error.