how to use object as array like in simplexml

how to use object as array like in simplexml

am 29.03.2008 17:54:45 von emmettnicholas

Hi,
I am new to PHP, and I'm trying to implement something similar to
SimpleXML. The following code demonstrates an example of what I'm
trying to achieve:

$xmlstr = <<
bar1
bar2

XML;

$xml = new SimpleXMLElement($xmlstr);

echo $xml->foo . "
";
echo $xml->foo[0] . "
";
echo $xml->foo[1] . "
";
echo $xml->foo[1]["att"] . "
";
?>

The output of this code is:

bar1
bar1
bar2
val2

What type of object is $xml->foo and how can I create a similar
object? It can be accessed as an array (e.g. $xml->foo[1]). But
echoing it doesn't print "Array" as usual, but rather apparently calls
the __toString method of $xml->foo[0].

Thanks a lot.

-Emmett

Re: how to use object as array like in simplexml

am 29.03.2008 23:33:56 von Jeremy

emmettnicholas@gmail.com wrote:
>
> What type of object is $xml->foo and how can I create a similar
> object? It can be accessed as an array (e.g. $xml->foo[1]). But
> echoing it doesn't print "Array" as usual, but rather apparently calls
> the __toString method of $xml->foo[0].
>
> Thanks a lot.
>
> -Emmett

You can add this support to your own class by implementing the
ArrayAccess interface. See

http://www.php.net/~helly/php/ext/spl/interfaceArrayAccess.h tml

The documentation is pretty poor, but it works something like this - you
implement each of the methods in the interface so that they do the
proper thing according to your access scheme. Here's a simple example
that just maps object array-style access to an internal array in the object:

class ArrayAccessExample implements ArrayAccess
{

private $internal = array();

public function offsetExists($offset)
{
// return true if the offset exists,
// false otherwise
return isset($this->internal[$offset]);
}

public function offsetGet($offset)
{
//return the specified offset
//what happens if it doesn't exist is
//up to you (return null, throw exception, etc)

if(!isset($this->internal[$offset]))
throw new Exception(
"Index {$offset} not defined"
);

return $this->internal[$offset];
}

public function offsetSet($offset, $value)
{
//set data at specified offset
$this->internal[$offset] = $value;

//not sure if you have to do this
//(as in C++ operator overloading) but it doesn't hurt
return $value;
}

public function offsetUnset($offset)
{
//unset given offset so that it no longer exists
unset($this->internal[$offset]);
}
}

$obj = new ArrayAccessExample;
$obj[0] = "test";
$obj['foo'] = "bar";

//etc


Hope that's helpful,
Jeremy

Re: how to use object as array like in simplexml

am 30.03.2008 04:09:07 von emmettnicholas

On Mar 29, 6:33 pm, Jeremy wrote:
> emmettnicho...@gmail.com wrote:
>
> > What type of object is $xml->foo and how can I create a similar
> > object? It can be accessed as an array (e.g. $xml->foo[1]). But
> > echoing it doesn't print "Array" as usual, but rather apparently calls
> > the __toString method of $xml->foo[0].
>
> > Thanks a lot.
>
> > -Emmett
>
> You can add this support to your own class by implementing the
> ArrayAccess interface. See
>
> http://www.php.net/~helly/php/ext/spl/interfaceArrayAccess.h tml
>
> The documentation is pretty poor, but it works something like this - you
> implement each of the methods in the interface so that they do the
> proper thing according to your access scheme. Here's a simple example
> that just maps object array-style access to an internal array in the object:
>
> class ArrayAccessExample implements ArrayAccess
> {
>
> private $internal = array();
>
> public function offsetExists($offset)
> {
> // return true if the offset exists,
> // false otherwise
> return isset($this->internal[$offset]);
> }
>
> public function offsetGet($offset)
> {
> //return the specified offset
> //what happens if it doesn't exist is
> //up to you (return null, throw exception, etc)
>
> if(!isset($this->internal[$offset]))
> throw new Exception(
> "Index {$offset} not defined"
> );
>
> return $this->internal[$offset];
> }
>
> public function offsetSet($offset, $value)
> {
> //set data at specified offset
> $this->internal[$offset] = $value;
>
> //not sure if you have to do this
> //(as in C++ operator overloading) but it doesn't hurt
> return $value;
> }
>
> public function offsetUnset($offset)
> {
> //unset given offset so that it no longer exists
> unset($this->internal[$offset]);
> }
>
> }
>
> $obj = new ArrayAccessExample;
> $obj[0] = "test";
> $obj['foo'] = "bar";
>
> //etc
>
> Hope that's helpful,
> Jeremy

Thank you, that's exactly what I was looking for.
-Emmett