RecursiveArrayIterator - Recreating jQuery $.extend -- fail
Posted: Tue Jul 26, 2011 9:17 pm
All,
I really don't know where to go from here. What I'm trying to do is recreate jQuery's $.extend method. Obviously, you'd have to be familiar with this, but I'm sure most will be...
Anyways, the issue I'm having is honestly just a lack of understanding with php's RecursiveArrayIterator. As you can see, I'm mainly just wanting to deal with array's.
The concept is:
Any help would seriously be welcomed, even if it is a url to a tutorial that I may have missed, code updates, or anything else... Thanks!
fyi, if anyone sees this: http://www.php.net/manual/en/function.a ... php#104739
This is not what i'm looking for.... I believe it can be done a little more elegant (not to knock that author's attempt...)
I really don't know where to go from here. What I'm trying to do is recreate jQuery's $.extend method. Obviously, you'd have to be familiar with this, but I'm sure most will be...
Anyways, the issue I'm having is honestly just a lack of understanding with php's RecursiveArrayIterator. As you can see, I'm mainly just wanting to deal with array's.
The concept is:
- define default values for all required keys with the $properties array
- $properties can be an unlimited multi-dimensional array
- User defines their own array, overriding only the key/value pairs that they want to define (see line below the class definition)
- If a key wasn't defined or set with value, the defaults are used instead.
Any help would seriously be welcomed, even if it is a url to a tutorial that I may have missed, code updates, or anything else... Thanks!
Code: Select all
<?php
class extend {
private static $properties = array("prop1" => "someVal",
"prop2" => array("subProp" => "someVal",
"subProp2" => "someVal",
),
"prop3" => array("subProp" => "someVal",
"subProp2" => "someVal",
"subProp3" => array("deepProp" => "someVal",
),
),
);
/**
* @author Jonathon Hibbard
* Merges passed properties with the default properties.
*
* @param RecursiveArrayIterator $arrayObj
* @param $key // This is used when we go to the second level...
*
* @todo make this an infinite recursive extend method...
*/
public static function defaults(RecursiveArrayIterator $arrayObj, $key = NULL) {
while($arrayObj->valid()) {
if($arrayObj->hasChildren()) {
self::extend($arrayObj->getChildren(), $arrayObj->key());
} else {
if(isset($key)) {
self::$properties[$key][$arrayObj->key()] = $arrayObj->current();
} else {
self::$properties[$arrayObj->key()] = $arrayObj->current();
}
}
$arrayObj->next();
}
}
}
$arrayObj = new RecursiveArrayIterator($myOverrideArray);
iterator_apply($arrayObj, 'extend::defaults', array($arrayObj)); // Merge our defaults with the passed properties...
?>
This is not what i'm looking for.... I believe it can be done a little more elegant (not to knock that author's attempt...)