温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PHP预定义接口:ArrayAccess

发布时间:2020-08-10 05:19:03 来源:网络 阅读:1364 作者:hgditren 栏目:web开发
简介
  • 提供像访问数组一样访问对象的能力的接口。

接口摘要
ArrayAccess {
/* 方法 */
abstract public boolean offsetExists ( mixed $offset )
abstract public mixed offsetGet ( mixed $offset )
abstract public void offsetSet ( mixed $offset , mixed $value )
abstract public void offsetUnset ( mixed $offset )
}

目录
  • ArrayAccess::offsetExists — 检查一个偏移位置是否存在
  • ArrayAccess::offsetGet — 获取一个偏移位置的值
  • ArrayAccess::offsetSet — 设置一个偏移位置的值
  • ArrayAccess::offsetUnset — 复位一个偏移位置的值

代码演示

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

    public function offsetExists($offset): bool
    {
        echo '调用' . __METHOD__ . '方法' . PHP_EOL;
        echo print_r(func_get_args(), true);

        return isset($this->container[$offset]);
    }

    public function offsetGet($offset)
    {
        echo '调用' . __METHOD__ . '方法' . PHP_EOL;
        echo print_r(func_get_args(), true);

        return $this->container[$offset] ?? null;
    }

    public function offsetSet($offset, $value)
    {
        echo '调用' . __METHOD__ . '方法' . PHP_EOL;
        echo print_r(func_get_args(), true);
        $this->container[$offset] = $value;
    }

    public function offsetUnset($offset)
    {
        echo '调用' . __METHOD__ . '方法' . PHP_EOL;
        echo print_r(func_get_args(), true);

        unset($this->container[$offset]);
    }
}

//实例化对象
$zhangsan = new Obj();

//赋值
$zhangsan['name'] = '张三';//调用Obj::offsetSet方法

//输出
echo $zhangsan['name'] . PHP_EOL;//调用Obj::offsetGet方法

//校验是否存在
isset($zhangsan['name']) . PHP_EOL;//调用Obj::offsetExists方法

//删除数组单元
unset($zhangsan['name']);//调用Obj::offsetUnset方法
逆向思考:

通过调用接口方法来实现对对象属性进行操作,然后使用数组的方式访问

$zhangsan=new Obj();

if ($zhangsan instanceof \ArrayAccess) {
    //通过调用接口方法为对象赋值
    $zhangsan->offsetSet('name', '张三');

    //通过数组方式取值
    echo $zhangsan['name'] . PHP_EOL;
    //通过调用接口方法取值
    echo $zhangsan->offsetGet('name').PHP_EOL;

    //通过调用接口方法来判断对象属性是否存在
    if (!$zhangsan->offsetExists('age') && !isset($zhangsan['age'])) {
        $zhangsan->offsetSet('age', '18');
    }

    $zhangsan['address']='北京';
    //通过调用接口方法来判断对象属性
    $zhangsan->offsetUnset('address');

    echo $zhangsan['address'] ?? 'address is not exists';

    //直接为对象属性赋值(不可行)
    $zhangsan->school='Peking University';
    echo $zhangsan['school'] ?? 'school is not exists';//school is not exists
}

应用案例演示

实现配置文件信息读取

准备工作:

按照一下给出的目录结构建立文件

./
├── config
│   ├── app.php
│   └── database.php
└── config.php
  • ./config/app.php

    <?php
    return [
        'name' => 'app name',
        'version' => 'v1.0.0'
    ];
  • ./config/database.php

    <?php
    return [
        'mysql' => [
            'host' => 'localhost',
            'user' => 'root',
            'password' => '12345678'
        ]
    ];
  • ./config.php
<?php

namespace Config;

final Class Config implements \ArrayAccess
{
    private $config = [];
    private static $instance = null;
    private $path = null;

    private function __construct()
    {
        $this->path = __DIR__ . '/config/';
    }

    //单例模式获取实例
    public static function getInstance()
    {
        if (!self::$instance instanceof self) {
            self::$instance = new self;
        }
        return self::$instance;
    }

    //防止被克隆
    private function __clone()
    {
    }

    public function offsetExists($offset)
    {
        return isset($this->config[$offset]);
    }

    public function offsetGet($offset)
    {
        if (!isset($this->config[$offset]) || empty($this->config[$offset])) {
            //装载配置文件
            $this->config[$offset] = require $this->path . $offset . '.php';
        }
        return $this->config[$offset];
    }

    public function offsetSet($offset, $value)
    {
        throw new \Exception('不提供设置配置');
    }

    public function offsetUnset($offset)
    {
        throw new \Exception('不提供删除配置');
    }
}

$config = Config::getInstance();

//获取app.php 文件的 name
echo $config['app']['name'].PHP_EOL; //app name

//获取database.php文件mysql的user配置
echo $config['database']['mysql']['user'].PHP_EOL; // root
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI