For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > March 2008 > how to use object as array like in simplexml









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how to use object as array like in simplexml
emmettnicholas@gmail.com

2008-03-29, 7:13 pm

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:

<?php
$xmlstr = <<<XML
<asdf>
<foo att="val1">bar1</foo>
<foo att="val2">bar2</foo>
</asdf>
XML;

$xml = new SimpleXMLElement($xmlstr);

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

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
Jeremy

2008-03-29, 7:13 pm

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/s...rrayAccess.html

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


emmettnicholas@gmail.com

2008-03-29, 10:16 pm

On Mar 29, 6:33 pm, Jeremy <jer...@pinacol.com> wrote:
> emmettnicho...@gmail.com wrote:
>
>
>
>
> You can add this support to your own class by implementing the
> ArrayAccess interface. See
>
> http://www.php.net/~helly/php/ext/s...rrayAccess.html
>
> 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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com