标签:
<?php
class obj implements arrayaccess
{
private $container = array();
public function __construct () {
$this -> container = array(
"one" => 1 ,
"two" => 2 ,
"three" => 3 ,
);
}
public function offsetSet ( $offset , $value )
{
echo ‘把对象当数组一样赋值的时候执行,此方法‘;
if ( is_null ( $offset ))
{
$this -> container [] = $value ;
}
else
{
$this -> container [ $offset ] = $value ;
}
}
public function offsetExists ( $offset )
{
echo ‘把对象当数组一样检测是否定义的时候执行,此方法‘;
return isset( $this -> container [ $offset ]);
}
public function offsetUnset ( $offset )
{
echo ‘把对象当数组一样删除元素的时候执行,此方法‘;
unset( $this -> container [ $offset ]);
}
public function offsetGet ( $offset )
{
echo ‘把对象当数组一样获取某元素的时候执行,此方法‘;
return isset( $this -> container [ $offset ]) ? $this -> container [ $offset ] : null ;
}
}
$obj = new obj ;
var_dump (isset( $obj [ "two" ]));
var_dump ( $obj [ "two" ]);
unset( $obj [ "two" ]);
var_dump (isset( $obj [ "two" ]));
$obj [ "two" ] = "A value" ;
var_dump ( $obj [ "two" ]);
$obj [‘a‘] = ‘Append 1‘ ;
$obj [‘b‘] = ‘Append 2‘ ;
$obj [‘c‘] = ‘Append 3‘ ;
print_r ( $obj );
标签:
原文地址:http://www.cnblogs.com/sixiong/p/5740672.html