This article mainly introduces simple examples of php initialization objects and destructor. If you need some help, please refer to them.
This article mainly introduces simple examples of php initialization objects and destructor. If you need some help, please refer to them.
The Code is as follows:
/*************************************** *******
* _ Construct object initialization function usage
* Use of destruct destructor
* $ This keyword is used to access the object attributes and system variables of the object * method in the current object)
*
**************************************** ******/
Header ("Content-Type: text/html; charset = UTF-8 ");
Class mypc {
Public $ name;
Public $ type;
Function _ construct ($ name = '', $ type ='') {// initialize the object and put the initialization value in brackets.
$ This-> name = $ name;
$ This-> type = $ type;
}
Function vod (){
Return $ this-> name. $ this-> type. 'play ';
}
Function game (){
Return $ this-> name. $ this-> type. 'player ';
}
/************************
* When the operation inside the object is completed,
* _ Destruct () is called,
* The memory used by the object is then released. Rule: Forward, forward, and forward
**************************/
Function _ destruct (){
Echo"
================ ". $ This-> name;
}
}
$ Pc1 = new mypc ('home pc', 'desktop ');
Echo $ pc1-> vod ()."
";
// When $ pc1 = null, the object is directly released after the current instance operation is complete.
$ Pic2 = new mypc ('Company id', 'laptop ');
Echo $ pic2-> game ();