PHP Classes
elePHPant
Icontem

Handle Inner Values: Trait to allow accessing objects like Python lists

Recommend this page to a friend!
  Info   View files Example   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2018-06-05 (1 year ago) RSS 2.0 feedNot enough user ratingsTotal: 48 All time: 9,575 This week: 462Up
Version License PHP version Categories
handle-inner-values 1.0.0The PHP License5.4PHP 5, Data types, Traits
Description Author

This package provides a trait to allow accessing objects like Python lists.

It provides a trait that provides functions for implementing the ArrayAccess Interface access objects like arrays.

Applications can pass a string to access elements of the object. The string should have a specific syntax that can specify elements of an object of a class that may contain other objects or arrays.

  Performance   Level  
Name: zinsou A.A.E.Mo´se is available for providing paid consulting. Contact zinsou A.A.E.Mo´se .
Classes: 50 packages by
Country: Benin Benin
Age: 29
All time rank: 8761 in Benin Benin
Week rank: 48 Up1 in Benin Benin Equal
Innovation award
Innovation award
Nominee: 23x

Winner: 2x

 

Details
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.

 
  Files folder image Files  
File Role Description
Accessible without login Plain text file example.php Example example script
Plain text file HandleInnerValues.trait.php Class trait source
Accessible without login Plain text file readme.txt Doc. readme

 Version Control Unique User Downloads Download Rankings  
 0%
Total:48
This week:0
All time:9,575
This week:462Up