PHP魔術(shù)方法的使用示例
① __get/__set:將對(duì)象的屬性進(jìn)行接管
當(dāng)訪問一個(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中訪問一個(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 fopen下載遠(yuǎn)程文件的函數(shù)
下面小編就為大家?guī)?lái)一篇淺談php fopen下載遠(yuǎn)程文件的函數(shù)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2016-11-11
Codeigniter上傳圖片出現(xiàn)“You did not select a file to upload”錯(cuò)誤解決辦法
這篇文章主要介紹了Codeigniter上傳圖片出現(xiàn)“You did not select a file to upload”的解決辦法,需要的朋友可以參考下2014-06-06
Laravel中表單size驗(yàn)證數(shù)字示例詳解
Laravel 的驗(yàn)證功能非常強(qiáng)大,基本上常見的需求都有對(duì)應(yīng)的驗(yàn)證規(guī)則,下面這篇文章主要給大家介紹了關(guān)于Laravel中表單size驗(yàn)證數(shù)字的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-07-07
php導(dǎo)出csv數(shù)據(jù)在瀏覽器中輸出提供下載或保存到文件的示例
這篇文章主要介紹了php導(dǎo)出csv數(shù)據(jù)在瀏覽器中輸出提供下載或保存到文件的示例,需要的朋友可以參考下2014-04-04

