作为程序员一定要保持良好的睡眠,才能好编程

实现ArrayAccess接口 可以使用数组形式调用类的属性

发布时间:2020-02-02


<?php


class Student implements ArrayAccess
{

    /** @var int 为了实现foreach 获取参数 而设置 */
    private $key = 0;
    private $current = 0; //当前所指向的指针

    public $param = [];

    public function __construct(string $name, int $age)
    {
        $this->param['name'] = $name;
        $this->param['age'] = $age;
    }

    /**
     * 修改器 设置数据对象的值
     * @access public
     * @param string $name 名称
     * @param mixed $value 值
     * @return void
     */
    public function __set($name, $value)
    {
        $this->param[$name] = $value;
    }

    /**
     * 获取器 获取数据对象的值
     * @access public
     * @param string $name 名称
     * @return mixed
     */
    public function __get($name)
    {
        return $this->param[$name] ?? null;
    }

    /**
     * 当对不可访问属性调用,isset 或 empty的时候 调用__isset
     * __isset
     * @param $name
     * @return bool
     *
     * @author songyz <songyz@guahao.com>
     * @date 2020/2/2 22:33
     */
    public function __isset($name)
    {
        return isset($this->param[$name]);
    }

    /**
     * 当在类外部使用unset()函数来删除私有成员时自动调用的
     * __unset
     * @param $name
     *
     * @author songyz <songyz@guahao.com>
     * @date 2020/2/2 22:43
     */
    public function __unset($name)
    {
        unset($this->param[$name]);
    }


    /**
     * @inheritDoc
     */
    public function offsetExists($offset)
    {
        return $this->__isset($offset);
    }

    /**
     * @inheritDoc
     */
    public function offsetGet($offset)
    {
        return $this->__get($offset);
    }

    /**
     * @inheritDoc
     */
    public function offsetSet($offset, $value)
    {
        $this->__set($offset, $value);
    }

    /**
     * @inheritDoc
     */
    public function offsetUnset($offset)
    {
        $this->__unset($offset);
    }

    /**
     *  echo $stu['name']; //实现这种方式获取参数
     * // ArrayAccess
     * public function offsetExists($offset)
     * {
     * return array_key_exists($offset, $this->items);
     * }
     *
     * public function offsetGet($offset)
     * {
     * return $this->items[$offset];
     * }
     *
     * public function offsetSet($offset, $value)
     * {
     * if (is_null($offset)) {
     * $this->items[] = $value;
     * } else {
     * $this->items[$offset] = $value;
     * }
     * }
     *
     * public function offsetUnset($offset)
     * {
     * unset($this->items[$offset]);
     * }
     */

//
//    /**
//     * 返回当前指针的值
//     * @inheritDoc
//     */
//    public function current()
//    {
//        return $this->param[$this->current];
//    }

//    /**
//     * 设置下次指针
//     * @inheritDoc
//     */
//    public function next()
//    {
//        ++$this->current;
//        // TODO: Implement next() method.
//    }
//
//    /**
//     * 返回当前指针
//     * @inheritDoc
//     */
//    public function key()
//    {
//        return $this->current;
//    }
//
//    /**
//     * @inheritDoc
//     */
//    public function valid()
//    {
//        return isset($this->param[$this->current]);
//        // 验证当前指针的值是否存在
//    }
//
//    /**
//     * 指针的重置
//     * @inheritDoc
//     */
//    public function rewind()
//    {
//        $this->key = 0;
//    }
}



调用实例:


$stu = new Student('james', '12');
//
//echo $stu->name . '   ' . $stu->age;
//
//var_dump(isset($stu->address)); //或触发__isset 方法
//var_dump(isset($stu->name)); //或触发__isset 方法
//
//print_r($stu->param);

//echo $stu['name'];


foreach ($stu as $key => $val) {

    var_dump($key);
    var_dump($val);

    //    echo $key . '=' . $val . "\n";
}