downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

ArrayAccess::offsetExists> <IteratorAggregate::getIterator
Last updated: Fri, 20 Nov 2009

view this page in

The ArrayAccess interface

Introduction

Interface to provide accessing objects as arrays.

Interface synopsis

ArrayAccess
ArrayAccess {
/* Methods */
abstract public boolean offsetExists ( mixed $offset )
abstract public mixed offsetGet ( mixed $offset )
abstract public void offsetSet ( mixed $offset , mixed $value )
abstract public void offsetUnset ( mixed $offset )
}

Example #1 Basic usage

<?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) {
        
$this->container[$offset] = $value;
    }
    public function 
offsetExists($offset) {
        return isset(
$this->container[$offset]);
    }
    public function 
offsetUnset($offset) {
        unset(
$this->container[$offset]);
    }
    public function 
offsetGet($offset) {
        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"]);

?>

The above example will output something similar to:

bool(true)
int(2)
bool(false)
string(7) "A value"

Table of Contents



add a note add a note User Contributed Notes
ArrayAccess
Cintix
29-Oct-2009 06:32
To take full advantages of all array features with ArrayAccess, then you would need to implements Countable and Iterator

Like this.

<?php

class ArrayOfColorModel implements ArrayAccess, Iterator, Countable {
    private
$container = array();

    public function
__construct() {
    }

    public function
offsetSet($offset,$value) {
         if (
$value instanceof ColorModel){
            if (
$offset == "") {
               
$this->container[] = $value;
            }else {
               
$this->container[$offset] = $value;
            }
        } else {
            throw new
Exception("Value have to be a instance of the Model ColorModel");
        }
    }

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

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

    public function
offsetGet($offset) {
        return isset(
$this->container[$offset]) ? $this->container[$offset] : null;
    }

    public function
rewind() {
       
reset($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
08-Jan-2009 10: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.

It is therefore not possible to take arbitrary code dealing with arrays and try to substitute an object of your own for an array, even if all of the normal array functions didn't fail as well (which they do, or at least some of them).

ArrayAccess::offsetExists> <IteratorAggregate::getIterator
Last updated: Fri, 20 Nov 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites