views:

110

answers:

3

Hi,

In java and C++ when we dont know the size - array not used like in php, instead used linkedList etc.. In php exist SPL, but most of the times programers use array, why(because peaple don't know about spl )?

When we should use in php5 array and when SPL and what the difference in this case between PHP and Java/C++?

Thanks

+1  A: 

Use standard arrays, it is faster than ArrayObject.

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

silent
Why does "it's slower" always have to be the first arguments against something?
salathe
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
+2  A: 

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
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..??
Yosef
Each time new element is created new memory for variable is allocated and new memory for hash is allocated.
codez
+3  A: 

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
I think i bit confuce avout SPL
Yosef