Analysis of Constructor and Destructor in PHP

  • 2021-07-21 08:01:58
  • OfStack

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's a good idea to do a little initialization 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 subclass. If the subclass does not define a constructor, it inherits from the parent class like a normal class method 1 (if it is not defined as private).

Example # 1 uses 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'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 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-fashioned constructor, that is, the function with the same name as the class. Therefore, the only case where compatibility issues arise is when a method named __construct () already exists in the class and is used for other purposes.

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

Since PHP 5.3. 3, methods with the same name as class names are no longer used as constructors in namespaces. This 1 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 destructor, which is similar to other object-oriented languages, such as C + +. The destructor executes when all references to an object are deleted or when the object is explicitly destroyed.

Example of Example # 3 Destructor


<?php
class MyDestructableClass {
   function __construct() {
       print "In constructor\n";
       $this->name = "MyDestructableClass";
   }    function __destruct() {
       print "Destroying " . $this->name . "\n";
   }
} $obj = new MyDestructableClass();
?>

Like constructor 1, the destructor of the parent class will not be secretly called by the engine. To execute the destructor of the parent class, you must explicitly call parent:: __destruct () in the destructor body of the subclass. Also like Constructor 1, a subclass inherits the parent if it does not define its own destructor.

The destructor is called even when the script is terminated using exit (). Calling exit () in the destructor will abort the rest of the shutdown operations.

Note:
The destructor is called when the script closes, when all HTTP headers have been emitted. The working directory when the script is closed may be different from that when it is in SAPI (such as apache).
Note:
Trying to throw an exception in the destructor (called when the script terminates) causes a fatal error.


Related articles: