Analysis of constructor and Destructor in PHP

Source: Internet
Author: User
This article mainly introduces constructor and Destructor parsing in PHP. This article uses code examples to explain constructor and Destructor in PHP. For more information, see. Constructor

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

The code is 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

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

The code is 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 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

The code is as follows:


<? Php
Class MyDestructableClass {
Function _ construct (){
Print "In constructor \ n ";
$ This-> name = "MyDestructableClass ";
}

Function _ destruct (){
Print "Destroying". $ this-> name. "\ n ";
}
}

$ Obj = new 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.