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基本語法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
php post大量數(shù)據(jù)時(shí)發(fā)現(xiàn)數(shù)據(jù)丟失問題解決方法
這篇文章主要介紹了php post大量數(shù)據(jù)時(shí)發(fā)現(xiàn)數(shù)據(jù)丟失問題解決方法,原因是默認(rèn)配置里的數(shù)據(jù)量配置太小造成的這個(gè)問題,修改一下配置即可,需要的朋友可以參考下2015-06-06
php中刪除字符串中最先出現(xiàn)某個(gè)字符的實(shí)現(xiàn)代碼
刪除字符串中最先出現(xiàn)某個(gè)字,就是通過explode的靈活用法,需要的朋友可以參考下2013-02-02
Laravel5.5+ 使用API Resources快速輸出自定義JSON方法詳解
這篇文章主要介紹了Laravel5.5+ 使用API Resources快速輸出自定義JSON方法詳解,需要的朋友可以參考下2020-04-04
發(fā)款php蜘蛛統(tǒng)計(jì)插件只要有mysql就可用
有時(shí)候我們?yōu)榱丝匆幌轮┲肱佬械那闆r,不得不對日志進(jìn)行大量的分析,由此想做一款插件可以記錄蜘蛛的情況。在第一次做的時(shí)候,只是記錄下蜘蛛的爬行次數(shù),不大好分析。2010-10-10
通過PHP自帶的服務(wù)器來查看正則匹配結(jié)果的方法
這篇文章主要介紹了通過PHP自帶的服務(wù)器來查看正則匹配結(jié)果的方法,通過這樣的方式可以在搬上服務(wù)器之前在工作環(huán)境上完成很多輕量級(jí)的試驗(yàn),需要的朋友可以參考下2015-12-12

