Yes, there are some standard PHP (Standard PHP Library) classes in PHP that do the job well.
Generally these classes implement the interfaces ArrayAccess
, Iterator
or IteratorAggregate
.
The ArrayAccess
interface allows access and definition of class members as if it were a array
.
Here are some examples:
SplStack
This class works like a stack. When you add an element via SplStack $stack[]
, instead of adding the element at the end as it does in an ordinary array, it adds to the beginning.
Example:
$stack = new SplStack;
$stack[] = 1;
$stack[] = 2;
$stack[] = 3;
print_r(iterator_to_array($stack));
This would return:
[
2 => 3,
1 => 2,
0 => 1,
]
SplFixedArray
The SplFixedArray
class is used to determine a fixed number of elements for a array
. You determine in the instance how many elements will be present in that structure. It is similar to the Array
of javascript, since you can not name the indexes, but the indexes are just numeric. If you try to place a larger-than-expected size, an exception will be thrown.
Example:
$arr = new SplFixedArray(3);
$arr[0] = 0;
$arr[1] = 1;
$arr[2] = 2;
Some people say that they did tests and got an economy 33% less than a% of common%, but that's the kind of thing I do not usually focus on.
SplObjectStorage
This class is for you to store objects. The big difference is that when you define an object in it, it is passed as the index of that "structure".
$s = new SplObjectStorage();
$o1 = new StdClass;
$o2 = new StdClass;
$o3 = new StdClass;
$s[$o1] = "data for object 1";
$s[$o2] = array(1,2,3);
These are some examples, but there are others, which have already been mentioned by @rray.