容器类的两种实现方式

原文地址:http://blog.csdn.net/koastal/article/details/72528456

通过魔术方法实现

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]);
        }
    }
}

使用

$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]);
        }
    }
}

使用

$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]);
        }
    }
}

使用

$container = new Container();
$container['logger'] = function ($msg){
    file_put_contents('info.log',$msg.PHP_EOL,FILE_APPEND);
};
$logger = $container->logger;
$logger('container works');

猜你喜欢

转载自blog.csdn.net/qq_32439101/article/details/79523544