Questions tagged [spl]

SPL is a collection of PHP interfaces and classes that are meant to solve standard problems. This tag is not to be confused with the [shakespeare-lang] tag for Shakespeare Programming Language

SPL stands for Standard PHP Library. It is a collection of PHP interfaces and classes that are meant to solve standard problems. It contains a large set of predefined iterators, data-structures, interfaces and exceptions that can be used and extended.

Documentation can be found here. It is available by default from PHP 5.0.0 onwards.

365 questions
324
votes
14 answers

How can I sort arrays and data in PHP?

This question is intended as a reference for questions about sorting arrays in PHP. It is easy to think that your particular case is unique and worthy of a new question, but most are actually minor variations of one of the solutions on this…
deceze
  • 510,633
  • 85
  • 743
  • 889
233
votes
4 answers

What is Autoloading; How do you use spl_autoload, __autoload and spl_autoload_register?

I am learning advanced PHP standards and trying to implement new and useful methods. Earlier I was using __autoload just to escape including multiple files on each page, but recently I have seen a tip on __autoload manual spl_autoload_register()…
xkeshav
  • 53,360
  • 44
  • 177
  • 245
95
votes
4 answers

How does RecursiveIteratorIterator work in PHP?

How does RecursiveIteratorIterator work? The PHP manual has nothing much documented or explained. What is the difference between IteratorIterator and RecursiveIteratorIterator?
varuog
  • 3,031
  • 5
  • 28
  • 56
56
votes
2 answers

Difference between DirectoryIterator and FileSystemIterator

PHP 5 introduced DirectoryIterator, and PHP 5.3 introduced FileSystemIterator. FileSystemIterator extends DirectoryIterator, but the documentation fails to say what extra features it brings. Can you tell the difference between DirectoryIterator and…
BenMorel
  • 34,448
  • 50
  • 182
  • 322
44
votes
3 answers

InvalidArgumentException vs UnexpectedValueException

When should I use InvalidArgumentException and when UnexpectedValueException? They look the same to me. Note that one extends LogicException and the other one extends RuntimeException, so the difference shouldn't be so subtle IMO.
ChocoDeveloper
  • 14,160
  • 26
  • 79
  • 117
36
votes
2 answers

array_map vs loop and operation

Using: for($i=1; $i<= 10000; ++$i) { $arrayOfNumbers[] = rand(1, 99999); } Can some explain why there is such a speed difference: array_map(array($maxHeap, 'insert'), $arrayOfNumbers); # Avg Time: 0.92856907844543s #…
Lizard
  • 43,732
  • 39
  • 106
  • 167
36
votes
5 answers

Why am I getting Fatal error when calling a parent's constructor?

I am extending one of the SPL (Standard PHP Library) classes and I am unable to call the parent's constructor. Here is the error I am getting: Fatal error: Cannot call constructor Here is a link to the SplQueue's documentation:…
Tonto McGee
  • 495
  • 1
  • 4
  • 6
35
votes
6 answers

OutOfRangeException vs. OutOfBoundsException

PHP defines two SPL exceptions for invalid keys: OutOfRangeException: Exception thrown when an illegal index was requested. This represents errors that should be detected at compile time. OutOfBoundsException: Exception thrown if a value is not a…
NikiC
  • 100,734
  • 37
  • 191
  • 225
35
votes
2 answers

Spl, ArrayObject, ArrayObject::STD_PROP_LIST

I'm trying to understand STD_PROP_LIST constant in the documentation but so far i didn´t understand it, and didn´t found any explanation :( The documentation has the following example: $a = new ArrayObject(array(),…
Fried Smurf
  • 489
  • 1
  • 5
  • 10
34
votes
3 answers

Associative Array versus SplObjectStorage

I'm working on code to manage a collection of unique objects. The first prototype of this code utilises an associative array, basically as that's the way I've always done it. However, I'm also keen on taking advantage of functionality that's been…
GordonM
  • 31,179
  • 15
  • 87
  • 129
34
votes
11 answers

Iterate in reverse through an array with PHP - SPL solution?

Is there an SPL Reverse array iterator in PHP? And if not, what would be the best way to achieve it? I could simply do $array = array_reverse($array); foreach($array as $currentElement) {} or for($i = count($array) - 1; $i >= 0; $i--) { } But is…
Sebastian Hoitz
  • 9,343
  • 13
  • 61
  • 77
31
votes
2 answers

multiple spl_autoload_register

what is/are the benefit(s) of having multiple spl_autoload_register example: spl_autoload_register('autoload_systems'); spl_autoload_register('autoload_thirdparties'); spl_autoload_register('autoload_services'); vs: using…
Gino Sullivan
  • 2,203
  • 18
  • 29
30
votes
6 answers

Peek ahead when iterating an array in PHP

Is it possible to "peek ahead" while iterating an array in PHP 5.2? For example, I often use foreach to manipulate data from an array: foreach($array as $object) { // do something } But I often need to peek at the next element while going through…
pako
  • 1,908
  • 4
  • 24
  • 40
29
votes
3 answers

How to use RegexIterator in PHP

I have yet to find a good example of how to use the php RegexIterator to recursively traverse a directory. The end result would be I want to specify a directory and find all files in it with some given extensions. Say for example only html/php…
Chris
  • 11,780
  • 13
  • 48
  • 70
29
votes
5 answers

"Indirect modification of overloaded element of SplFixedArray has no effect"

Why the following $a = new SplFixedArray(5); $a[0] = array(1, 2, 3); $a[0][0] = 12345; // here var_dump($a); produces Notice: Indirect modification of overloaded element of SplFixedArray has no effect in on line Is it a bug? How…
Desmond Hume
  • 8,037
  • 14
  • 65
  • 112
1
2 3
24 25