PHP容器類的兩種實(shí)現(xiàn)方式示例
本文實(shí)例講述了PHP容器類的兩種實(shí)現(xiàn)方式。分享給大家供大家參考,具體如下:
通過魔術(shù)方法實(shí)現(xiàn)
class
class MagicContainer{ private $ele; function __construct() { $this->ele = []; } function __set($name, $value) { $this->ele[$name] = $value; } function __get($name) { return $this->ele[$name]; } function __isset($name) { return isset($this->ele[$name]); } function __unset($name) { if(isset($this->ele[$name])){ unset($this->ele[$name]); } } }
usage
$container = new MagicContainer(); $container->logger = function ($msg){ file_put_contents('info.log',$msg.PHP_EOL,FILE_APPEND); }; $logger = $container->logger; $logger('magic container works');
通過ArrayAccess接口實(shí)現(xiàn)
class
class ArrayContainer implements ArrayAccess { private $elements; public function __construct() { $this->elements = []; } public function offsetExists($offset){ return isset($this->elements[$offset]); } public function offsetGet($offset){ if($this->offsetExists($offset)){ return $this->elements[$offset]; }else{ return false; } } public function offsetSet($offset, $value){ $this->elements[$offset] = $value; } public function offsetUnset($offset){ if($this->offsetExists($offset)){ unset($this->elements[$offset]); } } }
usage
$container = new ArrayContainer(); $container['logger'] = function ($msg){ file_put_contents('info.log',$msg.PHP_EOL,FILE_APPEND); }; $logger = $container['logger']; $logger('array container works');
Container
class
class Container implements ArrayAccess { private $elements; public function __construct() { $this->elements = []; } public function offsetExists($offset){ return isset($this->elements[$offset]); } public function offsetGet($offset){ if($this->offsetExists($offset)){ return $this->elements[$offset]; }else{ return false; } } public function offsetSet($offset, $value){ $this->elements[$offset] = $value; } public function offsetUnset($offset){ if($this->offsetExists($offset)){ unset($this->elements[$offset]); } } function __set($name, $value) { $this->elements[$name] = $value; } function __get($name) { return $this->elements[$name]; } function __isset($name) { return isset($this->elements[$name]); } function __unset($name) { if(isset($this->elements[$name])){ unset($this->elements[$name]); } } }
usage
$container = new Container(); $container['logger'] = function ($msg){ file_put_contents('info.log',$msg.PHP_EOL,FILE_APPEND); }; $logger = $container->logger; $logger('container works');
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語(yǔ)法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
php實(shí)現(xiàn)分頁(yè)功能的詳細(xì)實(shí)例方法
在本篇文章里小編給大家整理的是關(guān)于php實(shí)現(xiàn)分頁(yè)功能的詳細(xì)實(shí)例方法,有需要的朋友們可以學(xué)習(xí)下。2019-09-09九個(gè)你必須知道而且又很好用的php函數(shù)和特點(diǎn)
以下小編就為大家介紹一個(gè)九個(gè)你必須知道而且又很好用的php函數(shù)和特點(diǎn)。非常實(shí)用哦!需要的朋友可以過來參考下2013-08-08PHP實(shí)現(xiàn)生成Excel文件并導(dǎo)出的示例詳解
這篇文章主要為大家詳細(xì)介紹了PHP實(shí)現(xiàn)生成Excel文件并導(dǎo)出的方法,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)PHP有一定的幫助,需要的可以參考一下2023-01-01探討:如何使用PHP實(shí)現(xiàn)計(jì)算兩個(gè)日期間隔的年、月、周、日數(shù)
本篇文章是對(duì)使用PHP實(shí)現(xiàn)計(jì)算兩個(gè)日期間隔的年、月、周、日數(shù)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06php版微信公眾平臺(tái)回復(fù)中文出現(xiàn)亂碼問題的解決方法
這篇文章主要介紹了php版微信公眾平臺(tái)回復(fù)中文出現(xiàn)亂碼問題的解決方法,涉及php字符串編碼轉(zhuǎn)換的相關(guān)操作技巧,需要的朋友可以參考下2016-09-09PHP基于session.upload_progress 實(shí)現(xiàn)文件上傳進(jìn)度顯示功能詳解
這篇文章主要介紹了PHP基于session.upload_progress 實(shí)現(xiàn)文件上傳進(jìn)度顯示功能,結(jié)合實(shí)例形式分析了php5.4版本session.upload_progress特性實(shí)現(xiàn)文件上傳進(jìn)度顯示的相關(guān)操作技巧,需要的朋友可以參考下2019-08-08