Home > Article > Backend Development > PHP object-oriented constructor and destructor
This article mainly introduces the constructor and destructor of PHP object-oriented, which has certain reference value. Now I share it with you. Friends in need can refer to it
Constructor
For a class with a constructor, this method will be called every time it is instantiated, which is suitable for initialization work.
class MyClass { // 构造函数 public function __construct($str) { echo $str; } } // 实例化对象 $c1= new MyClass('abc');
Destructor
When all references to an object are deleted, or are explicitly destroyed, or when the program ends ,implement.
<?php class Myclass { public function __destruct(){ echo '对象被销毁了'; } } $obj = new MyClass(); // 销毁对象的2种方法 unset($obj); $obj = null; echo '<hr>';
Related recommendations:
php object-oriented encapsulation
php object-oriented Classes and instantiated objects
The above is the detailed content of PHP object-oriented constructor and destructor. For more information, please follow other related articles on the PHP Chinese website!