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