Constructors
void __construct ([Mixed $args [, $ ...]])
The PHP 5 allowable developer defines a method as a constructor in a class. Classes with constructors Call this method each time a new object is created, so it is ideal to do some initialization before working with the object.
Note: If a constructor is defined in a subclass, the constructor of its parent class is not implicitly invoked. To execute the constructor of the parent class, you need to call Parent::__construct () in the constructor of the subclass. If a subclass does not define a constructor, it inherits from the parent class as a normal class method (if it is not defined as private).
Example#1 using the new standard constructor
Copy Code code as follows:
<?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 ();
?>
Output
In BaseClass constructor
In BaseClass constructor
In subclass constructor
In BaseClass constructor
For backward compatibility, if PHP 5 cannot find the __construct () function in the class and does not inherit one from the parent class, it will try to find the legacy constructor, which is a function with the same name as the class. So the only scenario that will create a compatibility problem is when a method named __construct () already has a class in it that 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 than the parent class __construct ().
From PHP 5.3.3, in a namespace, a method with the same name as a 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
Copy Code code as follows:
<?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
}
}
?>
destructor
void __destruct (void)
PHP 5 introduces the concept of destructors, which is similar to other object-oriented languages, such as C + +. Destructors are executed when all references to an object are deleted or when an object is explicitly destroyed.
Example#3 destructor Example
Copy Code code as follows:
<?php
Class Mydestructableclass {
function __construct () {
Print "in constructor\n";
$this->name = "Mydestructableclass";
}
function __destruct () {
Print "destroying". $this->name. "\ n";
}
}
$obj = new Mydestructableclass ();
?>
As with constructors, the destructor of a parent class is not invoked by the engine. To perform a destructor for a parent class, you must explicitly call Parent::__destruct () in the destructor body of the subclass. In addition, as with constructors, subclasses inherit the parent class if they do not have a destructor defined themselves.
Destructors are invoked even when the script is run with exit (). Calling exit () in a destructor will abort the rest of the shutdown operation.
Note:
The destructor is invoked when the script is closed, at which point all HTTP header information has been sent. The working directory at the time the script is closed may be different from the one in SAPI (such as Apache).
Note:
An attempt to throw an exception in a destructor (invoked at the end of a script) can result in a fatal error.