Re: Re: ArrayObject - Posibillity to add key => value pair per default?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Jan 14, 2009 at 9:20 AM, Edmund Hertle <
edmund.hertle@xxxxxxxxxxxxxxx> wrote:

> > Edmund Hertle schrieb:
> >
> >  Hey,
> >> I've just discovered the ArrayObject class, but it seems to not be well
> >> documented, so here is my problem:
> >>
> >> You can use ArrayObject::append() to add a new value to the array, but
> is
> >> there also a method to add a new key and value?
> >>
> >> And I know that I could extend the class and write my own method but
> isn't
> >> this quite a base method for arrays in php? So maybe I just missed a
> >> obvious
> >> point?
> >>
> >> Quick example:
> >>
> >> Without ArrayObject:
> >> 1. $array[] = $value
> >> 2. $array[$key] = $value
> >>
> >> With ArrayObject:
> >> 1. $arrayObject->append($value)
> >> 2. ???
> >>
> >> -eddy
> >>
> >>  Hi Eddy,
> > use offsetSet( key, data) to do this. You can implements the Interface or
> > extend from the Class to extend the functionality (think interceptors
> __SET
> > and __GET)
> >
> > Regards
> >
> > Carlos Medina
>
> Thank you all. This is completly new to me (didn't thougt that it is
> possible to use [ ] on objects). So just for clarification:If I write a
> custom class (not an child of ObjectArray) and implement that array
> interface then I can use $customClass[$key] = $value and changing the
> functionality as I like to, for example create a nice usability?


all you have to do is implement ArrayAccess, then your objects can use the
array notation.  there are 4 methods youll need to define, per the
interface,

offsetExists($offset)
offsetGet($offset)
offsetSet($offset, $value)
offsetUnset($offset)

Or with an other point of view: Can I use those braces normally on every
> object?


no, however some internal classes that do not explicitly implement
ArrayAccess support array notation, such as SimpleXMLElement.

Class [ <internal:SimpleXML> <iterateable> class SimpleXMLElement implements
Traversable ]


> Or is this functionality introduced with implementing the array
> interface?
>

its ArrayAccess, and yes; however internal classes dont *need* to implement
it in order to expose the array notation, afaik.

-nathan

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux