Item 547233
What's the name of the SPL (Standard PHP Library) class that implements an array with a zero based numeric indexes?
Answer
SplFixedArray
The documentation (September 9, 2013) mentions its speed as the major advantage to use it over normal arrays.
This disturbs me a little, because this cannot be the reason to choose SplFixedArray
over a normal array. As the PHP community has shown its speed benefit is about factor 2. I'd be very happy with that increase in wage but factor 2 is not a big bother in software optimization if you ask me.
Of course, when my script has to deal with a million array entries SplFixedArray
will save me a measurable amount of time: say 3 over 6 seconds. But that's both too long in terms of Web requests anyway. SplFixedArray
is not going to help me here, I'd better start thinking over the architecture of my application.
Not to mention the drawbacks of using SplFixedArray
. SplFixedArray
implements the SPL array interfaces but it's still not an PHP array. Therefore it cannot be used in native PHP array functions. If you want to sort an SplFixedArray
you can't use sort()
to sort it but you're in for college lab class on sorting algorithms. And believe me: what you are going to come up with is not going to be faster than PHP's native sort()
. So there goes the speed advantage.
But are there any reasons to use SplFixedArray
?
Actually this would be a better exam question, and the answer is yes. The main advantage is that SplFixedArray
is a type. This means that you can use it as a base class to derive your own classes from it and that you can use it as a type hint in function arguments(*) for example.
And because the official documentation of SplFixedArray
is rather short I will not elaborate any further on this too. Let's just say it this way: SplFixedArray
is for the likes of us, array
is for the script kiddies.
(*) array
can be used for type hinting too to but a PHP array is so generic that in hardly qualifies as a (strong) type.