8

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++?

hakre
  • 193,403
  • 52
  • 435
  • 836
Ben
  • 25,389
  • 34
  • 109
  • 165
  • Related: [What are the benefits of using SPL ArrayObject, ArrayIterator, RecursiveArrayIterator instead of regular arrays?](http://stackoverflow.com/questions/4072927/what-are-the-benefits-of-using-spl-arrayobject-arrayiterator-recursivearrayite) (Nov 2010); [Benefits of ArrayAccess Interface in PHP?](http://stackoverflow.com/q/4319603/367456) (Nov 2010) – hakre Apr 18 '13 at 15:50

4 Answers4

4

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

codez
  • 1,381
  • 1
  • 18
  • 28
  • dinamic php array mean that each time that new element inserted created new array. Or for example php array have default size like 100 cells and she increase if array need more then 100 cells by multiply 2 etc..?? – Ben Jul 02 '10 at 14:35
  • Each time new element is created new memory for variable is allocated and new memory for hash is allocated. – codez Jul 02 '10 at 14:55
1

Use standard arrays, it is faster than ArrayObject.

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

silent
  • 3,843
  • 23
  • 29
  • 5
    Why does "it's slower" always have to be the first arguments against something? – salathe Jul 02 '10 at 16:16
  • 1
    http://www.php.net/manual/pt_BR/class.arrayobject.php#90371 check this comment for benchmark. Performance is a very important parameter in heavy application, sorry. – silent Jul 03 '10 at 10:18
1

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?

salathe
  • 51,324
  • 12
  • 104
  • 132
0

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.

Nadir
  • 695
  • 8
  • 12