PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > how to use object as array like in simplexml
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
how to use object as array like in simplexml

Réponse
 
LinkBack Outils de la discussion
Vieux 29/03/2008, 17h54   #1
emmettnicholas@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut how to use object as array like in simplexml

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
  Réponse avec citation
Vieux 29/03/2008, 23h33   #2
Jeremy
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to use object as array like in simplexml

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/sp...rayAccess.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 ful,
Jeremy


  Réponse avec citation
Vieux 30/03/2008, 03h09   #3
emmettnicholas@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to use object as array like in simplexml

On Mar 29, 6:33 pm, Jeremy <jer...@pinacol.com> 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/sp...rayAccess.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 ful,
> Jeremy


Thank you, that's exactly what I was looking for.
-Emmett
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h19.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10110 seconds with 11 queries