PHP Classes

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

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 52 All time: 10,666 This week: 455Up
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.

Picture of zinsou A.A.E.Moïse
  Performance   Level  
Name: zinsou A.A.E.Moïse <contact>
Classes: 50 packages by
Country: Benin Benin
Innovation award
Innovation award
Nominee: 23x

Winner: 2x

Example

<?php
require_once('HandleInnerValues.trait.php');
class
tada implements ArrayAccess{
    protected
$container=[];
    use
HandleInnerValues;
}
echo
'<pre>';
$tada=new tada();
$tada[]='papa';
$tada['["papa"][0][6]']='tada';
$tada['%[["papa"][0][6]]%']='tada';
for(
$i=0;$i<8;$i++){
   
$tada[]=$i;
}
print_r($tada);
print_r($tada['#%4:-3%#n']);
print_r($tada['#%papa:-3%#n']);//attention danger faites plutot
print_r($tada['#%papa:-3%#']);
unset(
$tada['#%4:-3%#n']);
print_r($tada);
$tada['#%3:0%#n']=range(1,7);
print_r($tada);


?>


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 (3)  
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

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:52
This week:0
All time:10,666
This week:455Up