首页 > 数据库技术 > 详细

PHP ArrayAccess 接口简单实例

时间:2021-07-16 10:35:40      阅读:19      评论:0      收藏:0      [点我收藏+]
/*
 * ArrayAccess :Interface to provide accessing objects as arrays.
 * 用访问数组的方式访问对象的
 */

class Foo implements ArrayAccess
{
    private $container = [];

    public function __construct()
    {
        $this->container = [
            ‘a‘=>1,
            ‘b‘=>2,
            ‘c‘=>3
        ];
    }

    public function offsetExists($offset)
    {
        // TODO: Implement offsetExists() method.
        return isset($this->container[$offset]);
    }

    public function offsetGet($offset)
    {
        // TODO: Implement offsetGet() method.
        return $this->container[$offset]??null;
    }

    public function offsetSet($offset, $value)
    {
        // TODO: Implement offsetSet() method.
        $this->container[$offset] = $value;
    }

    public function offsetUnset($offset)
    {
        // TODO: Implement offsetUnset() method.
        unset($this->container[$offset]);
    }

}

$foo = new Foo();
$foo[‘d‘] = 321;
$foo[‘a‘] = 123;
var_dump(isset($foo[‘ae‘]));//bool(false)
var_dump(isset($foo[‘a‘]));//bool(true)

  

PHP ArrayAccess 接口简单实例

原文:https://www.cnblogs.com/jinshao/p/15018208.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!