Php常用的魔術(shù)方法
Php提供了一系列的魔術(shù)方法,魔術(shù)方法在php中是常見的,在日常開發(fā)中必不可少的存在。它是php面對(duì)象時(shí)特有的特征。一般作用于對(duì)象重載,他們都是以雙下劃線開頭,很多方法都是成對(duì)出現(xiàn)。
__construct,__destruct
__constuct在構(gòu)建對(duì)像時(shí)調(diào)用方法;
__destruct銷毀和結(jié)束時(shí)調(diào)用此方法
__get,__set
給私有屬性或外部不存在的屬性賦值時(shí)調(diào)用此方法
__get讀取私有屬性或外部不存的屬性時(shí)調(diào)用此方法
__isset,__unset
__isset對(duì)私有問(wèn)或不存在的屬性調(diào)用isset()或empty()時(shí)被調(diào)用
__unset對(duì)私有或不存在的屬性進(jìn)行unset時(shí)被調(diào)用
__call,__callStatic
__call調(diào)用私有或不存在的方法時(shí)被調(diào)用
__callStatic調(diào)用私有或不存在的靜態(tài)方法時(shí)被調(diào)用
__sleep,__wakeup
__sleep當(dāng)使用serialize時(shí)被調(diào)用,當(dāng)你不需要保存大對(duì)象的所有數(shù)據(jù)時(shí)很有用
__wakeup當(dāng)使用unserialize時(shí)被調(diào)用,可用于做些對(duì)象的初始化操作
__clone
進(jìn)行對(duì)象clone時(shí)被調(diào)用,用來(lái)調(diào)整對(duì)象的克隆行為
__toString
當(dāng)一個(gè)類被轉(zhuǎn)換成字符串時(shí)被調(diào)用
__invoke
當(dāng)以函數(shù)方式調(diào)用對(duì)象時(shí)被調(diào)用
__set_state
當(dāng)調(diào)用var_export()導(dǎo)出類時(shí),此靜態(tài)方法被調(diào)用。用__set_state的返回值做為var_export的返回值。
__debuginfo
當(dāng)調(diào)用var_dump()打印對(duì)象時(shí)被調(diào)用(當(dāng)你不想打印所有屬性)適用于PHP5.6版本
<?php
class Test
{
public $var = 'test';
//構(gòu)造函數(shù),
public function __construct()
{
echo '__construct called'.PHP_EOL;
}
//對(duì)象的引用被刪除,銷毀,調(diào)用exit時(shí),腳本關(guān)閉調(diào)用
public function __destruct()
{
echo '__destruct called'.PHP_EOL;
}
//當(dāng)給私有屬性或不存在屬性賦值時(shí)被調(diào)用
public function __set($name, $value)
{
echo $name.'-'.$value;
echo '__set called'.PHP_EOL;
}
//讀取私有屬性或不存在屬性時(shí)被調(diào)用
public function __get($name)
{
echo $name;
echo '__get called'.PHP_EOL;
}
//調(diào)用私有屬性或不存在的方法時(shí)被調(diào)用
public function __call($name, $arguments)
{
echo $name . '-' . implode(',', $arguments);
echo '__call called'.PHP_EOL;
}
//調(diào)用私有屬性或不存在的靜態(tài)方法時(shí)被調(diào)用
public static function __callStatic($name, $arguments)
{
echo $name . '-' . implode(',', $arguments);
echo '__callStatic called'.PHP_EOL;
}
//對(duì)私有屬性或不存在的屬性調(diào)用isset()或empty()時(shí)被調(diào)用
public function __isset($name)
{
echo $name;
echo '__isset called'.PHP_EOL;
return true;
}
//對(duì)私有屬性問(wèn)或不存在的屬性進(jìn)行unset時(shí)被調(diào)用
public function __unset($name)
{
echo $name;
echo '__unset called'.PHP_EOL;
}
//serialize時(shí)被調(diào)用,當(dāng)你不需要保存大對(duì)象的所有數(shù)據(jù)時(shí)很有用
public function __sleep()
{
echo '__sleep called'.PHP_EOL;
return array('var1111111111');
}
//unserialize時(shí)被調(diào)用,用于對(duì)象的初始化
public function __wakeup()
{
echo '__wakeup called'.PHP_EOL;
$this->var = 'test after wakeup';
}
//當(dāng)一個(gè)類被轉(zhuǎn)換成字符串時(shí)被調(diào)用
public function __toString()
{
return '__toString called'.PHP_EOL;
}
//進(jìn)行對(duì)象clone時(shí)被調(diào)用,對(duì)象的克隆行為
public function __clone()
{
echo '__clone called'.PHP_EOL;
}
//當(dāng)以函數(shù)方式調(diào)用對(duì)象時(shí)被調(diào)用
public function __invoke()
{
echo '__invoke called'.PHP_EOL;
}
//當(dāng)調(diào)用var_export()導(dǎo)出類時(shí),此靜態(tài)方法被調(diào)用。用__set_state的返回值做為var_export的返回值。
public static function __set_state($arr)
{
return '__set_state called'.PHP_EOL;
}
//當(dāng)調(diào)用var_dump()打印對(duì)象時(shí)被調(diào)用(當(dāng)你不想打印所有屬性)適用于PHP5.6版本
public function __debuginfo($arr)
{
echo '__debuginfo called'.PHP_EOL;
return array(
'var' => 'test fro __debuginfo'
);
}
}
$m = new Magic(); //__construct()被調(diào)用
$m->not_exist_property = test; //__set()被調(diào)用
echo $m->not_exist_property;//__get()被調(diào)用
$m->abc(1,2,3); //__call()被調(diào)用
echo isset($m->not_exist_property); //__isset()被調(diào)用,返回bool值
unset($m->not_exist_property); //__unset()被調(diào)用
echo $tmp = serialize($m); //__sleep()被調(diào)用
unserialize($tmp); //__wakeup()被調(diào)用
$m1 = clone $m; //__clone()被調(diào)用,對(duì)象默認(rèn)是引用傳遞,使用clone
關(guān)鍵詞則可實(shí)現(xiàn)對(duì)象復(fù)制
$m(); //__invoke()
eval( '$m2 = ' . var_export ( $m , true ) . ';' );var_dump($m2);
var_dump($m);
//最后__destruct()被調(diào)用
/*
結(jié)果:
__construct called
not_exist_property-test__set called
not_exist_property__get called
abc-1,2,3__call called
not_exist_property__isset called
1not_exist_property__unset called
__sleep called
O:5:"Magic":1:{s:13:"var1111111111";N;}__wakeup called
__destruct called
__clone called
__invoke called
string(20) "__set_state called
"
class Magic#1 (1) {
public $var =>
string(4) "test"
}
__destruct called
__destruct called
網(wǎng)站題目:Php常用的魔術(shù)方法
本文來(lái)源:http://www.rwnh.cn/news11/176461.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、品牌網(wǎng)站設(shè)計(jì)、虛擬主機(jī)、Google、響應(yīng)式網(wǎng)站、定制網(wǎng)站
廣告
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源:
創(chuàng)新互聯(lián)