In PHP, constructors and destructors are used in the class, let me give you a detailed introduction of the use of constructors and destructors in the PHP class, there is a need to understand the friend can refer to.
Destructors
The PHP destructor works just as opposed to constructors, which are executed automatically when an object is instantiated, and destructors are executed automatically when the object is destroyed.
By default, PHP frees only the memory occupied by object properties, does not destroy object-related resources, and uses destructors to execute code after an object is used to clear memory and destroy objects from memory. The destructor __destruct () is structured as follows:
The code is as follows |
Copy Code |
function __destruct () { /* Class initialization code */ }
|
Destructors are automatically called by the system and cannot be taken with parameters.
Instance:
The code is as follows |
Copy Code |
Class des{ function __destruct () { echo "Run end, perform destructor"; } } $p =new des (); /* Instantiate class */ $sum = 0; for ($i =0; $i <10; $i + +) { $sum = $sum + $i; echo $sum. " "; } ?> |
Invocation of destructors
How does PHP invoke destructors? Destructors are called when the PHP script is no longer related to the object. If you want to explicitly destroy an object call destructor, you can assign any value to the variable that points to the object, usually assigning the variable to null or using the unset () function.
Instance:
The code is as follows |
Copy Code |
Class des{ function __destruct () { Echo "Object is destroyed, execution destructor "; } } $p =new des (); /* Instantiate class */ echo "program start "; Unset ($p); /* Destroy variable $p */ echo "Program End"; ?> |
constructor function
In the process of using classes, we sometimes need to instantiate multiple fields of an object as a parent, and if it is done manually, there are a lot of unpredictable problems, and it can be handy if you do it automatically during object creation.
PHP constructors are functions that are automatically executed when a class is instantiated, also called a constructor.
As with other functions, the name of the constructor is the fixed name "__construct", which is structured as follows:
The code is as follows |
Copy Code |
function __construct ([Argument1,argument2,argument3]) { /* Class initialization code */ } |
Instance:
The code is as follows |
Copy Code |
Class user{ Public $name; Private $password; Private $login; Public function __construct ($name, $password) { $this->name= $name; $this->password= $password; $this->login=time (); } function GetLogin () { Return (date (' M d,y ', $this->login)); } } $user =new User (' Marry ', ' 888888 '); echo "User name:". $user->name. " "; Print ("Access Time". $user->getlogin ()); ?> |
Calling the parent class constructor
Constructors can be called by the class, and PHP must use the parent keyword when calling the constructor of the parents, otherwise it will not be called automatically.
Instance:
The code is as follows |
Copy Code |
Class task1{ Public Function __construct () { echo "Today is Monday". " "; } } Class Task2 extends task1{ function __construct () { Parent::__construct (); /* Call Parent class constructor */ echo "I must work!"; } } New Task2; ?> |
http://www.bkjia.com/PHPjc/628822.html www.bkjia.com true http://www.bkjia.com/PHPjc/628822.html techarticle in PHP, constructors and destructors are all used in classes, let me give you a detailed introduction to the use of constructors and destructors in the PHP class, there is a need to know the friend ...