PHP容器类的两种实现方式示例

yipeiwu_com5年前PHP代码库

本文实例讲述了PHP容器类的两种实现方式。分享给大家供大家参考,具体如下:

通过魔术方法实现

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接口实现

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');

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

php中判断文件空目录是否有读写权限的函数代码

is_writable用来处理,记住 PHP 也许只能以运行 webserver 的用户名(通常为 \'nobody\')来访问文件。不计入安全模式的限制。 Example #1 is_...

smarty实现多级分类的方法

smarty实现多级分类的方法

本文实例讲述了smarty实现多级分类的方法。分享给大家供大家参考。具体分析如下: 这里简单的介绍一下利用php smarty 多级分类读出与循环方法,单循环很简单,但是多级就要复杂一点...

PHP常用的类封装小结【4个工具类】

本文实例讲述了PHP常用的类封装。分享给大家供大家参考,具体如下: 这4个类分别是Mysql类、 分页类、缩略图类、上传类。 Mysql类 <?php /** * M...

php如何实现只替换一次或N次

 我们都知道,在PHP里Strtr,strreplace等函数都可以用来替换,不过他们每次替换的时候都是全部替换,举个例子: "abcabbc",这个字符串如果使用上边的函数来...

检查url链接是否已经有参数的php代码 添加 ? 或 &amp;

比如分页,因为有些链接已经有参数了,在附加分页信息的时候不能把原有的参数丢掉,所以判断一下链接是否有参数,然后根据需要附加分页信息。 方法很简单: 复制代码 代码如下:((strpos(...