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

search for in the

ArrayObject::append> <ArrayIterator::valid
Last updated: Fri, 10 Oct 2008

view this page in

The ArrayObject class

Introduction

...

Class synopsis

ArrayObject
ArrayObject implements IteratorAggregate , Traversable , ArrayAccess , Countable {
/* Methods */
void ArrayObject::append ( mixed $newval )
int ArrayObject::count ( void )
ArrayIterator ArrayObject::getIterator ( void )
mixed ArrayObject::offsetGet ( mixed $index )
void ArrayObject::offsetSet ( mixed $index , mixed $newval )
}

Table of Contents



ArrayObject::append> <ArrayIterator::valid
Last updated: Fri, 10 Oct 2008
 
add a note add a note User Contributed Notes
ArrayObject
tony at tonyandcarol dot com
08-Oct-2008 09:49
My need was to create a java-like collection where I could store objects by their DB primary keys while having the standard stack capabilities of adding, retrieving, and removing objects from collection. ArrayObject didn't quite do what I need it to do so I extended it a little.

<?php
class GenericCollection extends ArrayObject{
    private
$data;
    function
__construct(){
       
$this->data = new ArrayObject();
    }
   
    function
addObject($_id, $_object){
       
$_thisItem = new CollectionObject($_id, $_object);
       
$this->data->offSetSet($_id, $_thisItem);
    }
    function
deleteObject($_id){
       
$this->data->offsetUnset($_id);
    }
    function
getObject($_id){
       
$_thisObject = $this->data->offSetGet($_id);
        return
$_thisObject->getObject();
    }
    function
printCollection() {
       
print_r($this->data);
    }
}

class
CollectionObject {
    private
$id;
    private
$object;
   
    function
__construct($_id, $_object){
       
$this->id = $_id;
       
$this->object = $_object;
    }
    function
getObject(){
        return
$this->object;
    }
    function
printObject() {
       
print_r($this);
    }
}
?>

Call it like so:

<?php
$u1
= new User/Data/Object (); //whatever, just an object.

$myCollection = new GenericCollection();
$myCollection->addObject(1, $u1);                   
print_r($myCollection->getObject(1));
?>

Now you have a simple and functional collection framework. Add methods in for specific types of sorting, we just didn't need anything other than primary key access. And you can add introspection into the collection object if you need to track what kind of an object it is.

tony@tonyandcarol.com
dave at csixty4 dot com
05-Sep-2008 10:28
If you want to use array functions on an ArrayObject, why not use iterator_to_array() to get a standard PHP array?  Do your operations on that array, then instantiate a new ArrayObject, passing it the array.

This might be a little slow on large ArrayObjects, but you'd have access to all of the array functions.
Anonymous
10-Aug-2008 08:17
Too bad the Array functions [1] are not available on this object… otherwise I would be using it all the time.

[1] http://nl.php.net/manual/en/ref.array.php

ArrayObject::append> <ArrayIterator::valid
Last updated: Fri, 10 Oct 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites