note 116057 added to arrayobject.construct
| From: | slegersjohn at gmail dot com | Date: | Tue, 04 Nov 2014 10:31:09 +0000 |
| Subject: | note 116057 added to arrayobject.construct | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-200779@lists.php.net to get a copy of this message | ||
I made a couple of improvements to Itar van der Berg's RecursiveArrayObject implementation.
* The '__construct' method now inherits the behavior of 'parent::__construct',
which is better for forward compatibility
* It now has a constant 'MULTI', which allows you to check on/off whether Arrays are
recursively processed by default
* __construct has an additional parameter $multi, which allows you to override the 'MULTI'
constant
* 'getArrayCopy' now also behaves recursively
* '__ToString' returns a var_export of the array data
<?php
/**
* @author Iltar van der Berg & John Slegers
* @version 3.0.0
*/
class RecursiveArrayObject extends \ArrayObject {
const MULTI = true;
public function __construct(Array $array, $multi = NULL) {
parent::__construct($array);
if (($multi === NULL && static::MULTI) || $multi) {
foreach ($this as $key => $value) {
if (is_array($value)) {
$this[$key] = new static($value);
}
}
}
}
public function getArrayCopy($multi = NULL) {
$array = parent::getArrayCopy();
if (($multi === NULL && static::MULTI) || $multi) {
foreach ($array as $key => $value) {
if (is_object($value) && is_array($parents = class_parents($value))
&& in_array('ArrayObject', $parents)) {
$array[$key] = $value->getArrayCopy();
}
}
}
return $array;
}
public function __ToString() {
return var_export($this->getArrayCopy(), true);
}
}
----
Server IP: 72.52.91.14
Probable Submitter: 81.82.196.9
----
Manual Page -- http://php.net/manual/en/arrayobject.construct.php
Edit -- https://master.php.net/note/edit/116057
Del: integrated -- https://master.php.net/note/delete/116057/integrated
Del: useless -- https://master.php.net/note/delete/116057/useless
Del: bad code -- https://master.php.net/note/delete/116057/bad+code
Del: spam -- https://master.php.net/note/delete/116057/spam
Del: non-english -- https://master.php.net/note/delete/116057/non-english
Del: in docs -- https://master.php.net/note/delete/116057/in+docs
Del: other reasons-- https://master.php.net/note/delete/116057
Reject -- https://master.php.net/note/reject/116057
Search -- https://master.php.net/manage/user-notes.php