Simple Introduction to PHP constructor usage

Source: Internet
Author: User
Tags constructor php file


Constructors and destructors

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

<?php
Class BaseClass {
I am a father of the constructor
function __construct () {
Print "in BaseClass constructor<br>";
}
}

I am a child class
Class Subclass extends BaseClass {
function __construct () {//child's constructor
Parent::__construct (); I perform a father's constructor first.
Print "in subclass constructor<br>";
}
}

I'm a kid, but I don't have my own constructor, so I'm using my father's
Class Othersubclass extends BaseClass {
Inherits BaseClass ' s constructor
}

I am a child, I have my own constructor, I abandon my father's. So I don't perform the print of the Father's constructor
Class Othersub2class extends BaseClass {
Inherits BaseClass ' s constructor
function __construct () {//child's constructor
Print "in Othersub2class constructor<br>";
}
}

In BaseClass constructor
$obj = new BaseClass ();

In BaseClass constructor
In subclass constructor
$obj = new Subclass ();

In BaseClass constructor
$obj = new Othersubclass ();

$obj = new Othersub2class ();

?>
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

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

When the PHP file is fully executed, the class goes into destruction, performs a destructor, and the constructor, on the contrary, the constructor enters execution when the class is created

Example #3 destructor Example

<?php
Class Mydestructableclass {
function __construct () {
Print "in constructor<br>";
$this->name = "Mydestructableclass";
}

function __destruct () {
Print "destroying". $this->name. "<br>";
}
}

$obj = new Mydestructableclass ();
Echo ' I write something <br> ';

?>
Output

In constructor
I write something
Destroying 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:

The

Attempt to throw an exception in the destructor (invoked at the end of the script) can 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.