Simple examples of PHP initialization objects and destructors _php tips

Source: Internet
Author: User
Copy Code code as follows:

<?php
/**********************************************
* __construct Object initialization function uses
* Use of destruct destructors
* $this use of keywords ($this keywords are system variables used to access object properties and Object * methods in the current object)
*
**********************************************/
Header ("Content-type:text/html;charset=utf-8");
Class mypc{
Public $name;
Public $type;

function __construct ($name = ', $type = ') {//Initialize object, enclose initialization value in parentheses
$this->name= $name;
$this->type= $type;

}
Function VOD () {
Return $this->name. $this->type. ' Play a movie ';
}
function Game () {
Return $this->name. $this->type. ' Play the game ';
}
/************************
* When the internal operation of the object is completed,
* __DESTRUCT () is called,
* Then the memory used by the object is released. Rule: Backward First out
**************************/
function __destruct () {
echo "<br>==============". $this->name;
}
}

$PC 1 = new MyPC (' home computer ', ' Desktop ');
echo $pc 1->vod (). <br> ";
When the $pc1=null is completed, the object is released directly after the current instance operation completes
$pic 2 = new MyPC (' Company computer ', ' notebook ');
echo $pic 2->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.