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

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 

* @date 2020/2/2 22:33

*/

public function __isset($name)

{

return isset($this->param[$name]);

}

/**

* 当在类外部使用unset()函数来删除私有成员时自动调用的

* __unset

* @param $name

*

* @author songyz 

* @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";

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值