復(fù)制代碼 代碼如下:
<?php
/**********************************************
* __construct 對象初始化函數(shù)使用
* destruct 析構(gòu)函數(shù)的使用
* $this 關(guān)鍵字的使用($this關(guān)鍵字是用來訪問當前對象中的對象屬性和對象 * 方法的系統(tǒng)變量)
*
**********************************************/
header("Content-Type:text/html;charset=UTF-8");
class mypc{
public $name;
public $type;
function __construct($name='',$type=''){ //初始化對象,將初始化值放在括號內(nèi)
$this->name=$name;
$this->type=$type;
}
function vod(){
return $this->name.$this->type.'播放電影';
}
function game(){
return $this->name.$this->type.'玩游戲';
}
/************************
* 當對象內(nèi)部的操作執(zhí)行完畢的時候,
* __destruct()被調(diào)用,
* 然后對象所使用的內(nèi)存被釋放出來。規(guī)則:后進先出
**************************/
function __destruct(){
echo "<br>==============".$this->name;
}
}
$pc1 = new mypc('家用電腦','臺式機');
echo $pc1->vod()."<br>";
//當$pc1=null;的時候是當前實例操作完成后直接釋放出對象
$pic2 = new mypc('公司電腦','筆記本');
echo $pic2->game();
新聞熱點
疑難解答