Parsing of constructors and destructors in PHP, PHP constructor parsing _php Tutorial

Source: Internet
Author: User

Constructor and destructor parsing in PHP, PHP constructor parsing


constructor function

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

Copy the 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 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

Copy the 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
}
}
?>

Destructors

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
Copy the 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 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.


What is the relationship between constructors and destructors in PHP?

Constructor: __construct () executes the function while the class instantiates the object, and the destructor: __distruct () executes when the class-instantiated object is destroyed.

Help PHP What is the difference between a constructor function and a destructor?

Constructor: Called when an object is initialized
destructor: Called when an object is destroyed

http://www.bkjia.com/PHPjc/891104.html www.bkjia.com true http://www.bkjia.com/PHPjc/891104.html techarticle constructor and destructor parsing in PHP, PHP constructor parsing constructor void __construct ([Mixed $args [, $ ...]) PHP 5 allows the developer to define a method in a class as ...

  • 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.