成人精品一区二区三区中文字幕-成人精品一区二区三区-成人精品一级毛片-成人精品亚洲-日本在线视频一区二区-日本在线视频免费

導航首頁 ? 技術教程 ? PHP魔術方法的使用示例
全站頭部文字 我要出現在這里
PHP魔術方法的使用示例 696 2023-12-10   

① __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


主站蜘蛛池模板: cctv5+体育赛事直播时间| 布莱克·莱弗利身高体重| 钟绍图| 人民的名义演员名单表| 成人的性行为免费| 4438x五月天| 扒开双腿猛进入小柔的扮演者| 合作协议书合同| 那些年,那些事 电视剧| 梦的衣裳| 电影《19号海滩》在线播放国语版| 偷偷藏不住演员表| 陈颖芝三级| 第一财经今日股市直播间在线直播| hereweare| 视频污网站| 上海爱情故事| 北京卫视节目单全天| 《牵牛花》阅读答案| 蒋昌义| 夜生活女王之霞姐| 春香传在线观看| 女孩阴道| 人民的利益电视剧在线观看| cctv16体育节目表今天目表| 永远的紫荆花简谱| laizi| 男狐聊斋| 战狼7| 《身边有特点的人》作文| 殷明珠| 赌侠 1990 刘德华| 那些女人演员表全部名单| 蛇谷奇兵 电影| 孙婉| 台版缉魂2小时10分版网飞版| 小镇姑娘高清在线观看| 羞羞的动漫在线观看| 古天乐电影大全| 守株待兔评课| 单色美甲图片|

!!!站長長期在線接!!!

網站、小程序:定制開發/二次開發/仿制開發等

各種疑難雜癥解決/定制接口/定制采集等

站長微信:lxwl520520

站長QQ:1737366103