PHP魔術(shù)方法的使用示例
① __get/__set:將對(duì)象的屬性進(jìn)行接管
當(dāng)訪問(wèn)一個(gè)不存在的對(duì)象屬性時(shí):
index.php
<?php
define('BASEDIR',__DIR__); //定義根目錄常量
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\\Common\\Loader::autoload');
$obj = new \Common\Object();
//在php中訪問(wèn)一個(gè)不存在的對(duì)象屬性時(shí)
echo $obj->title;
會(huì)拋出一個(gè)錯(cuò)誤:Notice: Undefined property: Common\Object::$title in D:\practise\php\design\psr0\index.php on line 9
當(dāng)在Common/Object.php 中添加 __set 和 __get 方法后
Object.php
<?php
namespace Common;
class Object{
function __set($key,$value){
}
function __get($key){
}
}
再執(zhí)行 index.php,不會(huì)再報(bào)錯(cuò)。
再次修改 Common/Object.php
<?php
namespace Common;
class Object{
protected $array = array();
function __set($key,$value){
var_dump(__METHOD__);
$this->array[$key] = $value;
}
function __get($key){
var_dump(__METHOD__);
return $this->array[$key];
}
}
index.php
<?php
define('BASEDIR',__DIR__); //定義根目錄常量
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\\Common\\Loader::autoload');
$obj = new \Common\Object();
$obj->title = 'hello';
echo $obj->title;
執(zhí)行 index.php,頁(yè)面輸出:
string 'Common\Object::__set' (length=20)
string 'Common\Object::__get' (length=20)
hello
② __call/__callStatic:控制 PHP 對(duì)象方法的調(diào)用(__callStatic 用來(lái)控制類的靜態(tài)方法)
當(dāng)執(zhí)行一個(gè)不存在的php方法時(shí)
index.php:
<?php
define('BASEDIR',__DIR__); //定義根目錄常量
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\\Common\\Loader::autoload');
$obj = new \Common\Object();
//當(dāng)執(zhí)行一個(gè)不存在的php方法時(shí)
$obj->test('hello',123);
執(zhí)行 index.php 會(huì)報(bào)一個(gè)致命錯(cuò)誤:Fatal error: Call to undefined method Common\Object::test() in D:\practise\php\design\psr0\index.php on line 9
如果在 Common/Object 中定義一個(gè)__call 方法,則會(huì)在方法不存在時(shí)自動(dòng)回調(diào):
<?php
namespace Common;
class Object{
function __call($func, $param){ //$func 方法名 $param 參數(shù)
var_dump($func, $param);
return "magic function\n"; //返回一個(gè)字符串作為返回值
}
}
index.php
<?php
define('BASEDIR',__DIR__); //定義根目錄常量
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\\Common\\Loader::autoload');
$obj = new \Common\Object();
//當(dāng)執(zhí)行一個(gè)不存在的php方法時(shí)
echo $obj->test('hello',123);
頁(yè)面輸出:
string 'test' (length=4)
array
0 => string 'hello' (length=5)
1 => int 123
magic function
當(dāng)調(diào)用一個(gè)不存在的靜態(tài)方法時(shí)
Common/Object.php
<?php
namespace Common;
class Object{
static function __callStatic($name, $arguments) {
var_dump($name, $arguments);
return "magic function\n"; //返回一個(gè)字符串作為返回值
}
}
注意:__callStatic 方法也要聲明成靜態(tài)方法
index.php
<?php
define('BASEDIR',__DIR__); //定義根目錄常量
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\\Common\\Loader::autoload');
//執(zhí)行一個(gè)不存在的靜態(tài)方法
echo Common\Object::test("hello",1234);
執(zhí)行 index.php ,頁(yè)面輸出:
string 'test' (length=4)
array
0 => string 'hello' (length=5)
1 => int 1234
magic function
③ __toString:將一個(gè) PHP 對(duì)象轉(zhuǎn)換成一個(gè)字符串
index.php
<?php
define('BASEDIR',__DIR__); //定義根目錄常量
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\\Common\\Loader::autoload');
$obj = new \Common\Object();
echo $obj;
此時(shí)會(huì)報(bào)錯(cuò): Catchable fatal error: Object of class Common\Object could not be converted to string in D:\practise\php\design\psr0\index.php on line 8
在 Object.php 中添加 __toString 方法
<?php
namespace Common;
class Object{
function __toString() {
return __CLASS__;
}
}
④ __invoke:將一個(gè) PHP 對(duì)象當(dāng)成一個(gè)函數(shù)來(lái)執(zhí)行時(shí),會(huì)回調(diào)此魔術(shù)方法
index.php
<?php
define('BASEDIR',__DIR__); //定義根目錄常量
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\\Common\\Loader::autoload');
$obj = new \Common\Object();
echo $obj("test");
Object.php
<?php
namespace Common;
class Object{
function __invoke($param) {
var_dump($param);
return 'invoke';
}
}
頁(yè)面輸出:
string 'test' (length=4)
invoke
相關(guān)文章
PHP實(shí)現(xiàn)財(cái)務(wù)審核通過(guò)后返現(xiàn)金額到客戶的功能
有這么一個(gè)返現(xiàn)的系統(tǒng),當(dāng)前端客戶發(fā)起提現(xiàn)的時(shí)候,后端就要通過(guò)審核這筆返現(xiàn)訂單,才可以返現(xiàn)到客戶的賬號(hào)里。這篇文章主要介紹了PHP實(shí)現(xiàn)財(cái)務(wù)審核通過(guò)后返現(xiàn)金額到客戶 ,需要的朋友可以參考下2019-07-07淺談使用 PHP 進(jìn)行手機(jī) APP 開(kāi)發(fā)(API 接口開(kāi)發(fā))
做過(guò) API 的人應(yīng)該了解,其實(shí)開(kāi)發(fā) API 比開(kāi)發(fā) WEB 更簡(jiǎn)潔,但可能邏輯更復(fù)雜,因?yàn)?API 其實(shí)就是數(shù)據(jù)輸出,不用呈現(xiàn)頁(yè)面,所以也就不存在 MVC(API 只有 M 和 C),那么我們來(lái)探討下,如何使用php進(jìn)行手機(jī)API接口開(kāi)發(fā)2014-08-08再談Yii Framework框架中的事件event原理與應(yīng)用
這篇文章主要介紹了再談Yii Framework框架中的事件event原理與應(yīng)用,結(jié)合實(shí)例形式分析了再談Yii框架中的事件event相關(guān)原理、使用方法及操作注意事項(xiàng),需要的朋友可以參考下2020-04-04mysql查找刪除重復(fù)數(shù)據(jù)并只保留一條實(shí)例詳解
這篇文章主要介紹了mysql查找刪除重復(fù)數(shù)據(jù)并只保留一條實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2016-09-09php數(shù)組轉(zhuǎn)換js數(shù)組操作及json_encode的用法詳解
php數(shù)組轉(zhuǎn)換js數(shù)組操作及json_encode的用法。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-10-10PHP循環(huán)輸出指定目錄下的所有文件和文件夾路徑例子(簡(jiǎn)單實(shí)用)
這篇文章主要介紹了一個(gè)簡(jiǎn)單實(shí)用的PHP循環(huán)輸出指定目錄下的所有文件和文件夾路徑例子,需要的朋友可以參考下2014-05-05laravel接管Dingo-api和默認(rèn)的錯(cuò)誤處理方式
今天小編就為大家分享一篇laravel接管Dingo-api和默認(rèn)的錯(cuò)誤處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10