php 的arrayaccess抽象类的使用

作者: hopevow | 来源:发表于2016-10-20 11:02 被阅读51次

php中有一个arrayaccess抽象类其中有四个抽象方法

  • offsetSet($offset, $value)
  • offsetGet($offset)
  • offsetExists($offset)
  • offsetUnset($offset)

实现arrayaccess后可对对象进行数组式的操作,上代码:

<?php
class async implements arrayaccess {
    private $func = false;
    private $arg = false;
    private $data = false;

    public function __construct($func, $arg = []) {
        $this->func = $func;
        $this->arg = $arg;
    }

    public function __call($f, $a = []) {
        if ($this->data === false) {
            $this->data = call_user_func_array($this->func, $this->arg);
        }
        return call_user_func_array([$this->data, $f], $a);
    }

    public function __get($name) {
        if ($this->data === false) {
            $this->data = call_user_func_array($this->func, $this->arg);
        }
        return $this->data->$name;
    }

    public function offsetGet($offset) {
        if ($this->data === false) {
            $this->data = call_user_func_array($this->func, $this->arg);
        }

        if (isset($this->data[$offset])) {
            return $this->data[$offset];
        }

        throw new Exception($offset,1);
    }

    public function offsetSet($offset, $value) {
        if ($this->data === false) {
            $this->data = call_user_func_array($this->func, $this->arg);
        }
        $this->data[$offset] = $value;
    }

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

    public function offsetUnset($offset) {
        unset($this->data[$offset]);
    }
}

class myclass {
    public $arg1 = 'hello';
    public $arg2 = 'world';

    function __construct($other) {
        $this->other = $other;
    }

    public function say () {
        echo $this->arg1 . $this->arg2;
    }
}

$asyncobj = new async(function($arg){return new myclass($arg);}, ['我是一个测试参数']);
$asyncarray = new async(function(){return array();});
$asyncarray['myarg'] = 'add new argument;';
echo $asyncarray['myarg'];
$asyncarray->myarg = 'outer args';
echo $asyncarray->myarg;

这个类既继承了arrayaccess,也封装了前面 介绍的延迟加载,取决于你传入的参数;

案例输出

输出结果

相关文章

  • php 的arrayaccess抽象类的使用

    php中有一个arrayaccess抽象类其中有四个抽象方法 offsetSet($offset, $value)...

  • PHP中的ArrayAccess用法

    laravel框架中有很多实现了ArrayAccess这个接口,那么这个接口有什么用呢,看字面意思就是像数组一样使...

  • PHP底层ArrayAcces抽象类巧用分析

    php arrayaccess 官方的说法是让你能以数组的形式访问对象,对于这种php内置接口一直不太明白有什么用...

  • 如何以像数组一样的方式使用一个类

    在php中,有时候我们想像数组一样使用类来进行增删改查,那么正确的方法应该是让相应类实现ArrayAccess接口...

  • PHP抽象类

    PHP抽象类应用的定义:abstract class ClassName {} PHP抽象类应用要点: 1.定义...

  • 自动加载配置文件

    ArrayAccess::offsetGet 主要是处理\ArrayAccess 的 offsetGet() 方...

  • 把一个PHP对象当成数组来访问

    PHP预定义接口之 ArrayAccess(数组访问式) 一个实现了这个接口的类就可以做到了

  • PHP ArrayAccess实现程序配置化

    提供像访问数组一样访问对象的能力的接口。 接口摘要如下: 我们要实现项目配置 像数组一样去访问。1、定义Confi...

  • PHP 中的预定义接口

    介绍 PHP 中内置了一些预定义接口,接口实现后会获得一些特有的功能。 数组式访问(ArrayAccess) 提供...

  • abstract关键字

    抽象类 PHP5 支持抽象类和抽象方法。抽象类不能被实例化,必须先继承该抽象类,然后实例化该抽象类的子类。抽象类中...

网友评论

本文标题:php 的arrayaccess抽象类的使用

本文链接:https://www.haomeiwen.com/subject/kesfuttx.html