Home  >  Article  >  Backend Development  >  Principle analysis of constructors and destructors

Principle analysis of constructors and destructors

王林
王林forward
2020-07-10 17:56:492895browse

Principle analysis of constructors and destructors

Constructor

__construct ([ mixed $args [, $... ]] ) : void

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.

(Recommended tutorial: php tutorial)

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

Code example:

<?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();
?>

For backward compatibility, if PHP 5 cannot find a __construct() function in a class and does not inherit one from a parent class, it will Try looking for old-style constructors, which are functions with the same name as the class. So the only time a compatibility issue arises is when a 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.

Code example:

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

__destruct ( void ) : void

PHP 5 introduces 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 have been deleted or when the object is explicitly destroyed.

Code example:

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

  function __destruct() {
    print "Destroying " . $this->name . "\n";
  }
}

$obj = new MyDestructableClass();

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.

The above is the detailed content of Principle analysis of constructors and destructors. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete