Home > Backend Development > PHP Tutorial > php: Constructor and destructor parsing

php: Constructor and destructor parsing

黄舟
Release: 2023-03-12 10:46:02
Original
1306 people have browsed it

This article mainly introduces the constructor and destructor analysis in PHP. This article uses code examples to explain the constructor and destructor in PHP Function, friends who need it can refer to

Constructor

void construct ([ mixed $args [, $... ]] )
Copy after login

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#1Using the new standard constructor

<?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&#39;s constructor
}
 
// In BaseClass constructor
$obj = new BaseClass();
 
// In BaseClass constructor
// In SubClass constructor
$obj = new SubClass();
 
// In BaseClass constructor
$obj = new OtherSubClass();
?>
Copy after login

output:
In BaseClass constructor
In BaseClass constructor
In SubClass constructor
In BaseClass constructor

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 old-style constructor, that is, the one with the same name as the class function. So the only time a compatibility issue arises 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, the 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

<?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
    }
}
?>
Copy after login

Destructor

void destruct(void)
Copy after login

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

<?phpclass MyDestructableClass {   
function construct() {       
print "In constructor\n";       
$this->name = "MyDestructableClass";   
}
   function destruct() {       
   print "Destroying " . $this->name . "\n";   
   }}
$obj = new MyDestructableClass();?>
Copy after login

Like the constructor, the destructor of the parent class will not be called secretly 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 the script is terminated using exit(). Calling exit() in the destructor will abort the remaining shutdown operations.

Note:
The destructor is called when the script is closed, after 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.

The above is the detailed content of php: Constructor and destructor parsing. For more information, please follow other related articles on the PHP Chinese website!

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