免费邮箱 |加入收藏 | 会员中心 | 我要投稿 | RSS
您当前的位置:首页 > PHP专区 > PHP技巧

ArrayAccess(数组式访问)接口

时间:2011-05-16 15:50:51  来源:站内  作者:潘春会
($this->container);
    }

    public function
current() {
        return
current($this->container);
    }

    public function
key() {
        return
key($this->container);
    }

    public function
next() {
        return
next($this->container);
    }

    public function
valid() {
        return
$this->current() !== false;
    }   

    public function
count() {
     return
count($this->container);
    }

}

?>

Now you can using it like any other array.

<?php
   
   $array
= new ArrayOfColorModel();
   foreach (
$array as $model) {
       
var_export($model);
   }

  
// OR

  
for($i=0;$i<count($array);$i++){
       
var_export($array[$i]);
   }

?>

AryehGregor+php-comment at gmail dot com (09-Jan-2009 09:33)

 

 

 

Note that at least in PHP 5.1, objects implementing ArrayAccess cannot return objects by reference.  See http://bugs.php.net/bug.php?id=34783 .  If you have code like

<?php
$x
= &$y[0];
?>

then this will (as far as I can tell) *always* fail unless $y is a real array -- it cannot work if $y is an object implementing ArrayAccess.  If your offsetGet() function returns by reference, you get the fatal error "Declaration of MyClass::offsetGet() must be compatible with that of ArrayAccess::offsetGet()".  If you try to have it return by value, however, you get the (contradictory) fatal error "Objects used as arrays in post/pre increment/decrement must return values by reference", at least in my version of PHP.
来顶一下
返回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
推荐资讯
如何找出DHCP地址池里未使用的IP地址
如何找出DHCP地址池里
国内常用的DNS列表
国内常用的DNS列表
Linux邮件服务器软件比较
Linux邮件服务器软件比
学用纯CSS打造可折叠树状菜单
学用纯CSS打造可折叠树
相关文章
栏目更新
栏目热门