① __get/__set:將對象的屬性進行接管
當訪問一個不存在的對象屬性時:
index.php
<?php define('BASEDIR',__DIR__); //定義根目錄常量 include BASEDIR.'/Common/Loader.php'; spl_autoload_register('\Common\Loader::autoload');$obj = new CommonObject();
//在php中訪問一個不存在的對象屬性時 echo $obj->title;
會拋出一個錯誤:Notice: Undefined property: CommonObject::$title in D:practisephpdesignpsr0index.php on line 9
當在Common/Object.php 中添加 __set 和 __get 方法后
Object.php
<?php namespace Common;class Object{ function __set($key,$value){ } function __get($key){ } }
再執行 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 CommonObject();
$obj->title = 'hello'; echo $obj->title;
執行 index.php,頁面輸出:
string 'CommonObject::__set' (length=20) string 'CommonObject::__get' (length=20) hello
② __call/__callStatic:控制 PHP 對象方法的調用(__callStatic 用來控制類的靜態方法)
當執行一個不存在的php方法時
index.php:
<?php define('BASEDIR',__DIR__); //定義根目錄常量 include BASEDIR.'/Common/Loader.php'; spl_autoload_register('\Common\Loader::autoload');$obj = new CommonObject();
//當執行一個不存在的php方法時 $obj->test('hello',123);
執行 index.php 會報一個致命錯誤:Fatal error: Call to undefined method CommonObject::test() in D:practisephpdesignpsr0index.php on line 9
如果在 Common/Object 中定義一個__call 方法,則會在方法不存在時自動回調:
<?php namespace Common;class Object{ function __call($func, $param){ //$func 方法名 $param 參數 var_dump($func, $param); return "magic functionn"; //返回一個字符串作為返回值 } }
index.php
<?php define('BASEDIR',__DIR__); //定義根目錄常量 include BASEDIR.'/Common/Loader.php'; spl_autoload_register('\Common\Loader::autoload');$obj = new CommonObject();
//當執行一個不存在的php方法時 echo $obj->test('hello',123);
頁面輸出:
string 'test' (length=4) array 0 => string 'hello' (length=5) 1 => int 123 magic function
當調用一個不存在的靜態方法時
Common/Object.php
<?php namespace Common;class Object{ static function __callStatic($name, $arguments) { var_dump($name, $arguments); return "magic functionn"; //返回一個字符串作為返回值 } }
注意:__callStatic 方法也要聲明成靜態方法
index.php
<?php define('BASEDIR',__DIR__); //定義根目錄常量 include BASEDIR.'/Common/Loader.php'; spl_autoload_register('\Common\Loader::autoload');//執行一個不存在的靜態方法 echo CommonObject::test("hello",1234);
執行 index.php ,頁面輸出:
string 'test' (length=4) array 0 => string 'hello' (length=5) 1 => int 1234 magic function
③ __toString:將一個 PHP 對象轉換成一個字符串
index.php
<?php define('BASEDIR',__DIR__); //定義根目錄常量 include BASEDIR.'/Common/Loader.php'; spl_autoload_register('\Common\Loader::autoload');$obj = new CommonObject();
echo $obj;
此時會報錯: Catchable fatal error: Object of class CommonObject could not be converted to string in D:practisephpdesignpsr0index.php on line 8
在 Object.php 中添加 __toString 方法
<?php namespace Common;class Object{ function __toString() { return __CLASS__; } }
④ __invoke:將一個 PHP 對象當成一個函數來執行時,會回調此魔術方法
index.php
<?php define('BASEDIR',__DIR__); //定義根目錄常量 include BASEDIR.'/Common/Loader.php'; spl_autoload_register('\Common\Loader::autoload');$obj = new CommonObject();
echo $obj("test");
Object.php
<?php namespace Common;class Object{ function __invoke($param) { var_dump($param); return 'invoke'; } }
頁面輸出:
string 'test' (length=4) invoke