Simple Example of php initialization object and destructor

Source: Internet
Author: User
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 ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.