Question

In java and C++ when we don't know the size - array not used like in PHP, instead used linkedList etc.

In PHP exist SPL, but most of the times programmers use array, why (because people don't know about SPL )?

When we should use Array in PHP and whenSPL and what is the difference in this case between PHP and Java/C++?

Was it helpful?

Solution

Every PHP request must initialize all variables and after request they are freed. Because of that not often comes situations where special data structures (like maxheap, linkedlist or queue) are more efficient than array. Also arrays are much simpler to understand and use for beginner.

Difference from C++ in PHP is that arrays length is dynamic. You can add elements whenever you want.

$arr=array();
$arr[]=5; //add integer to array
echo count($arr); //1
$arr[]=7;
echo count($arr); //2

you can dynamically create and add array to another array

$arr[]=array();
$arr[2][]=5;
echo count($arr); //3
echo count($arr[2]); //1

This will create new array, add element with value 5 and add it as element to array $arr.

$arr[][]=5;

In PHP arrays are hash tables, so you can have not only integer keys but also strings:

$arr['somekey']='somevalue';

If array element is integer then each element requires a value structure (zval) which takes 16 bytes. Also requires a hash bucket - which takes 36 bytes. That gives 52 bytes per value. Memory allocation headers take another 8 bytes*2 - which gives 68 bytes.

About arrays in PHP: http://oreilly.com/catalog/progphp/chapter/ch05.html

OTHER TIPS

You're asking us to compare two massively different things, almost the only similarity being that they're both (arrays and the SPL) available in PHP.

To that end, it would be essentially nonsensical to compare directly, or prescribe, when one should be used over the other for times when both might be used to accomplish a task. On that note, both might be used intertwined: for example, using the ArrayIterator to iterate over an array, or the ArrayObject to make use of array-style syntax when working with objects.

You also seem to be confused, or just unclear, about what the SPL is; it certainly is not restricted to tools used to iterate over collections of things. Did you mean to ask about specific parts of the library, or are you perhaps just unclear what is available in it?

Use standard arrays, it is faster than ArrayObject.

Use ArrayObject only to implement your own specified arrays with your custom methods.

Not necessarilly a confusion maybe, because people can decide to use "SplStack" instead of vanilla array associated with array_push/pop functions. Or use arrays in classical way,

and then there exist SplDoublyLinkedList, SplQueue, SplHeap. plus ArrayObject.

So the question is valid. And the answer is: you do the same in a more javaish way, and it is slower.

SPL generally speaking is a tentative to mimic standard & "off-the-shelf" classes & API, like there exist in C++ & Java.

IMHO it came too late.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top