note 111192 added to arrayobject.construct
| From: | kjarli at gmail dot com | Date: | Thu, 24 Jan 2013 14:48:49 +0000 |
| Subject: | note 111192 added to arrayobject.construct | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-193373@lists.php.net to get a copy of this message | ||
As of PHP 5.3 I recommend to use this, I have replace "new self" by "new
static". This allows you to extend this class by your own and let your class create itself
recursively without overwriting the constructor.
<?php
/**
* @author Iltar van der Berg
* @version 2.0.0
*/
class RecursiveArrayObject extends ArrayObject
{
/**
* overwrites the ArrayObject constructor for
* iteration through the "array". When the item
* is an array, it creates another self() instead
* of an array
*
* @param Array $array data array
*/
public function __construct(Array $array)
{
foreach($array as $key => $value) {
if(is_array($value)){
$value = new static($value);
}
$this->offsetSet($key, $value);
}
}
/**
* returns Array when printed (like "echo array();")
* instead of an error
*
* @return string
*/
public function __ToString()
{
return 'Array';
}
}
?>
----
Server IP: 69.147.83.199
Probable Submitter: 91.184.7.3
----
Manual Page -- http://php.net/manual/en/arrayobject.construct.php
Edit -- https://master.php.net/note/edit/111192
Del: integrated -- https://master.php.net/note/delete/111192/integrated
Del: useless -- https://master.php.net/note/delete/111192/useless
Del: bad code -- https://master.php.net/note/delete/111192/bad+code
Del: spam -- https://master.php.net/note/delete/111192/spam
Del: non-english -- https://master.php.net/note/delete/111192/non-english
Del: in docs -- https://master.php.net/note/delete/111192/in+docs
Del: other reasons-- https://master.php.net/note/delete/111192
Reject -- https://master.php.net/note/reject/111192
Search -- https://master.php.net/manage/user-notes.php