Php constructor and Destructor

Source: Internet
Author: User
Void _ construct ([mixed $ args [, $...]) PHP5 allows developers to define a method as a constructor in a class. Classes with constructors call this method each time a new object is created, so it is very suitable for initialization before using the object. Note: If the constructor is defined in the subclass, the constructor of its parent class is not implicitly called. To execute

Constructor struct ¶

Void _ construct ([mixed $ args [, $...])

PHP 5 allows developers to define a method in a class as a constructor. Classes with constructors call this method each time a new object is created, so it is very suitable for initialization before using the object.

Note: If the constructor is defined in the subclass, the constructor of its parent class is not implicitly called. To execute the constructor of the parent class, you must call parent ::__ construct () in the constructor of the subclass (). If the subclass does not define the constructor, it will inherit from the parent class as a normal class method (if not defined as private ).

Example #1 use the new standard constructor

class BaseClass{function __construct(){print "In BaseClass constructor
";}}class SubClass extends BaseClass{function __construct(){parent::__construct();print "In SubClass constructor
";}}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

To implement 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 old constructor, that is, a function with the same name as a class. Therefore, the only situation that causes compatibility problems is that a method named _ construct () in the class is used for other purposes.

Unlike other methods, PHP does not generate an E_STRICT error message when _ construct () is overwritten by methods with different parameters from the parent class _ construct.

Since PHP 5.3.3, methods with the same name as the class name in the namespace are no longer used as constructors. 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 the PHP5.3.0-5.3.2 // A common method from PHP5.3.3 }}

Destructor struct ¶

Void _ destruct (void)

PHP 5 introduces the concept of destructor, which is similar to other object-oriented languages, such as C ++. The Destructor is executed when all references to an object are deleted or when the object is explicitly destroyed.

Example #3 destructor Example

 ";$this->name = "MyDestructableClass";}function __destruct(){print "Destroying ".$this->name.'
';}}$obj = new MyDestructableClass();?>

Output result:

In constructor

Destroying MyDestructableClass

Like constructors, the parent class's destructor will not be called by the engine. To execute the destructor of the parent class, you must explicitly call parent ::__ destruct () in the destructor of the subclass (). In addition, just like constructors, if the subclass does not define the destructor, it will inherit the parent class.

The destructor will be called even when the exit () termination script is used. Calling exit () in the destructor will stop other close operations.

Note:

The Destructor is called when the script is disabled. all HTTP header information is sent. The working directory when the script is disabled may be different from that in SAPI (such as apache.

Note:

An exception thrown in the destructor (called when the script is terminated) will cause 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.