note 81508 added to language.oop5.iterations
| From: | (nospam) | Date: | Sat, 01 Mar 2008 22:50:53 +0000 |
| Subject: | note 81508 added to language.oop5.iterations | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-136646@lists.php.net to get a copy of this message | ||
By reading the posts below I wondered if it really is impossible to make an ArrayAccess
implementation really behave like a true array ( by being multi level )
Seems like it's not impossible. Not very preety but usable
<?php
class ArrayAccessImpl implements ArrayAccess {
private $data = array();
public function offsetUnset($index) {}
public function offsetSet($index, $value) {
// echo ("SET: ".$index."<br>");
if(isset($data[$index])) {
unset($data[$index]);
}
$u = &$this->data[$index];
if(is_array($value)) {
$u = new ArrayAccessImpl();
foreach($value as $idx=>$e)
$u[$idx]=$e;
} else
$u=$value;
}
public function offsetGet($index) {
// echo ("GET: ".$index."<br>");
if(!isset($this->data[$index]))
$this->data[$index]=new ArrayAccessImpl();
return $this->data[$index];
}
public function offsetExists($index) {
// echo ("EXISTS: ".$index."<br>");
if(isset($this->data[$index])) {
if($this->data[$index] instanceof ArrayAccessImpl) {
if(count($this->data[$index]->data)>0)
return true;
else
return false;
} else
return true;
} else
return false;
}
}
echo "ArrayAccess implementation that behaves like a multi-level array<hr />";
$data = new ArrayAccessImpl();
$data['string']="Just a simple string";
$data['number']=33;
$data['array']['another_string']="Alpha";
$data['array']['some_object']=new stdClass();
$data['array']['another_array']['x']['y']="LOL @
Whoever said it can't be done !";
$data['blank_array']=array();
echo "'array' Isset? "; print_r(isset($data['array'])); echo
"<hr />";
echo "<pre>"; print_r($data['array']['non_existent']); echo
"</pre>If attempting to read an offset that doesn't exist it returns a blank object!
Use isset() to check if it exists!<br>";
echo "'non_existent' Isset? ";
print_r(isset($data['array']['non_existent'])); echo "<br />";
echo "<pre>"; print_r($data['blank_array']); echo "</pre>A
blank array unfortunately returns similar results :(<br />";
echo "'blank_array' Isset? "; print_r(isset($data['blank_array']));
echo "<hr />";
echo "<pre>"; print_r($data); echo "</pre> (non_existent remains in the
structure. If someone can help to solve this I'll appreciate it)<hr />";
echo "Display some value that exists:
".$data['array']['another_string'];
?>
(in the two links mentioned below by artur at jedlinski... they say you can't use references,
so I didn't used them.
My implementation uses recursive objects)
If anyone finds a better (cleaner) sollution, please e-mail me.
Thanks,
Wave.
----
Server IP: 69.147.83.197
Probable Submitter: 86.122.116.66
----
Manual Page -- http://www.php.net/manual/en/language.oop5.iterations.php
Edit -- https://master.php.net/note/edit/81508
Del: integrated -- https://master.php.net/note/delete/81508/integrated
Del: useless -- https://master.php.net/note/delete/81508/useless
Del: bad code -- https://master.php.net/note/delete/81508/bad+code
Del: spam -- https://master.php.net/note/delete/81508/spam
Del: non-english -- https://master.php.net/note/delete/81508/non-english
Del: in docs -- https://master.php.net/note/delete/81508/in+docs
Del: other reasons-- https://master.php.net/note/delete/81508
Reject -- https://master.php.net/note/reject/81508
Search -- https://master.php.net/manage/user-notes.php