php constructor and destructor

伊谢尔伦
Release: 2016-11-23 14:18:47
Original
993 people have browsed it

Constructor ¶

void__construct ([ mixed$args [, $... ]] )

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

Note: If a constructor is defined in a subclass, the constructor of its parent class will not be implicitly called. To execute the parent class's constructor, you need to call parent::__construct() in the child class's constructor. If the subclass does not define a constructor, it will be inherited from the parent class like an ordinary class method (if it is not defined as private).

Example #1 Using the new standard constructor

class BaseClass
{
function __construct(){
print "In BaseClass constructor<br>";
}
}
class SubClass extends BaseClass
{
function __construct(){
parent::__construct();
print "In SubClass constructor<br>";
}
}
class OtherSubClass extends BaseClass
{}
$obj = new BaseClass();
$obj = new SubClass();
$obj = new OtherSubClass();
Copy after login

Output result:

In BaseClass constructor

In BaseClass constructor

In SubClass constructor

In BaseClass constructor

For backward compatibility, if PHP 5 If the __construct() function is not found in the class and it does not inherit one from the parent class, it will try to find an old-style constructor, that is, a function with the same name as the class. Therefore, the only situation where compatibility issues will arise is when the class already has a method named __construct() but it is used for other purposes.

Unlike other methods, PHP will not generate an E_STRICT error message when __construct() is overridden by a method with different parameters than the parent class __construct().

Since PHP 5.3.3, in the namespace, methods with the same name as the class name 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(){
//在PHP5.3.0-5.3.2中被看作是构造函数
//自PHP5.3.3起被视作一个普通的方法
}
}
Copy after login

Destructor ¶

void__destruct ( void )

PHP 5 introduced the concept of destructor, which is similar to other object-oriented languages ​​such as C++. A destructor is executed when all references to an object are removed or when the object is explicitly destroyed.

Example #3 Destructor example

<?php
class MyDestructableClass{
function __construct(){
print "In constructor<br>";
$this->name = "MyDestructableClass";
}
function __destruct(){
print "Destroying ".$this->name.&#39;<br>&#39;;
}
}
$obj = new MyDestructableClass();
?>
Copy after login

Output result:

In constructor

Destroying MyDestructableClass

Like the constructor, the destructor of the parent class will not be secretly called by the engine. To execute the parent class's destructor, parent::__destruct() must be explicitly called in the child class's destructor body. In addition, just like the constructor, the subclass will inherit the parent class if it does not define a destructor.

The destructor is called even when exit() is used to terminate the script. Calling exit() in the destructor will abort the rest of the shutdown operation.

Note:

The destructor is called when the script is closed, when all HTTP headers have been sent. It is possible that the working directory when the script is closed is different from when it is in a SAPI (such as apache).

Note:

Attempting to throw an exception in the destructor (which is called when the script terminates) will result in a fatal error.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template