PHP Basics: Classes and Objects (3) constructors and destructors _php techniques

Source: Internet
Author: User
Constructors
The PHP 5 allowable developer defines a method as a constructor in a class. Classes with constructors Call this method the first time you create an object, so it is ideal to do some initialization before using the object.
Attention:
If a constructor is defined in a subclass, it does not implicitly invoke the constructor of its parent class. To execute the constructor of the parent class, you need to call Parent::__construct () in the constructor of the subclass. (?? Obviously different from other languages??

Example 10.8. Using the new standard constructor
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 backward compatibility, if PHP 5 does not find the __construct () function in the class, it tries to look for a legacy constructor, which is a function with the same name as the class. So the only thing that will create a compatibility problem is that there is already a method named __construct () in the class, but it is not a constructor.

destructor
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 10.9. destructor examples
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. (?? Obviously different from other languages??
Attention:
The destructor is invoked when the script is closed, at which point all header information has been sent.
Attention:
Attempting to throw an exception in a destructor can result in a fatal error.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.