Since PHP doesn’t provide real arrays, I want to create an array class myself. However, preferably I don’t want to create some sort of wrapper class for the PHP array, but I really want to create a real array, like in C/C++. However, if I don’t want to use the build in PHP array, I need to have some way to allocate memory manually, and to find the size of an object/class. So I need a PHP version of the c functions sizeof and malloc. Unfortunately, I couldn’t find those in PHP. Are there functions which do the same thing, or do you know of any other way to do this?
Edit: I think my question isn’t clear enough:
In PHP an array is really an order map. In some situations this can make things a little more complicated: for example, if you have an array with indices 0,1,2 and 3, you can’t relay on that you will get those items in that order in a foreach loop: it depends on the order in which you defined the items in the array. Since those little things annoy me, I want to create a ‘real’ array class, based on arrays like they are in c/c++. That means that the arrays should have a fixed size, and that I need to allocate memory manually when an array is initialized. For example, when an array of 10 integers is created, the array class should allocate a memory block of the size 10*sizeof(integer). Of course I could use the build in PHP ‘array’ to store these 10 integers, but than I’ve just created a wrapper class for the build in array type. So preferably I want to be able to allocate that memory myself so I can manage it in a better way that the build in array type does. However, I couldn’t find a function which gives me the size of a class/object, and I could find a function which could allocate memory manually, like malloc does in c.
So my question is: are there any functions for this? And if they aren’t, do you have any other efficient ways to achieve my goal?
PHP already has an Array Class with
SplFixedArraywhich behaves like you want:gives
and finally
There is a couple of additional data structures in SPL. For an overview, see
If you want to know how
SplFixedArrayis implemented in PHP, have a look atIf you want anything different, you will probably have to implement that at the C level of the language, e.g. you’d have to write your own extension.