few time ago and many times yet i have realized that Objects implementing ArrayAccess interface are really cool
But it lacks something.
For example : you can't do this on an Object implementing ArrayAccess interface
$object[][][]=0;
This fact really frustrated me more than once. Of course you can not do this too
unset($object['something]['somethingelse']);
But more than once I needed these features.And more than once i implemented the same code to allow these features but via some
methods.
Each time I implemented ArrayAccess on one of my class I irremediably remark the lack.
So decided to put these methods in one trait to avoid repetitive programming.
Of course I still was frustrated because the bracket access is cool than some methods
so i finally make those methods private and find the way to use the brackets.
But once again I was not happy ,I needed more power and this is at this time that i remember Python list variable type
where you can use $object[1:9] to get something like array_slice($object,$offset,$length).
Finally i build the present Trait to bring some super powers to all objects which implement the
ArrayAccess interface And i decided to put it online for those like me who need these Super powers
too -:).
Keep in mind that your class must implements the ArrayAccess interface to be able to use this trait optimally
Except the basic access type like $object[]=2; and $object[1]=0 or unset($object[1])
you can now use
$tada['["papa"][0][6]']='tada'; //=> mean $tada["papa"][0][6]='tada';
unset($tada['["papa"][0][6]']); //=>mean unset($tada["papa"][0][6]);
print_r($tada['#%4:-3%#n']);//=>mean print_r(array_slice($tada,from offset 4 to length($tada)-3))
print_r($tada['#%papa:-3%#']);//=>mean print_r(array_slice($tada,from offset papa to length($tada)-3))
print_r($tada['#%papa:3%#']);//=>mean print_r(array_slice($tada,from offset papa for 3))
$tada['#%3:0%#n']=range(1,7);// =>mean insert the values of range(1,7) starting from offset 3 without deleting anything(array_splice)
$tada['#%3:6%#n']=range(1,7);// =>mean replace value from offset 3 by the values of range(1,7) for length 6(array_splice)
unset('#%4:-3%#n'])//mean=> unset value starting from offset 4 to length($tada)-3)
unset('#%4:3%#n'])//mean=> unset value starting from offset 4 for length 3
Note the n behind the last # which means that the specified offset is integer
finally if you wish to use in you offset element like [,# or % you must use the delimiter %[ and ]% so
anything between these tags will be treat as simple string.
Note that in Python you can do $tada[:9] or $tada[9:] here too and you can even just use
$tada[:] or $tada['papa'] to use the splice and slice functions but you must always specify it as string between the specific tags #% and %#
See the example file for more informations or use the forum.
|