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
23
votes
1 answer

PHP SPL, is it worth using or raw array functions are better?

I'm examining the Standard PHP Library (SPL). I used only arrays before and just now found that PHP has so many standard classes. But there is no any words in the manual whether it is recommended to use it or not. For example, they explicitly…
Green
  • 28,742
  • 61
  • 158
  • 247
21
votes
7 answers

What is the point of PHP's SplDoublyLinkedList class, and more importantly, Linked Lists in general?

On a quest to expand my programming prowess, I've delved ever-so-slightly into The Standard PHP Library. This led to my discovery of the SplDoublyLinkedList class. From there I read the descriptions of Linked Lists and Doubly Linked Lists on…
Stephen
  • 18,827
  • 9
  • 60
  • 98
20
votes
6 answers

How is SplSubject/SplObserver useful?

The Standard PHP Library includes what some resources call a reference implementation of the Observer pattern, by way of the SplSubject and SplObserver classes. For the life of me, I can't figure out how these are very useful with no way to pass…
FtDRbwLXw6
  • 27,774
  • 13
  • 70
  • 107
19
votes
4 answers

Does PHP 5.x have some kind of HashSet or Set Class?

I'm used to Java where I have HashSets, ArrayLists and other Collections. But I'm workting on a PHP project right now. I need to create a set, fill that set with objects (Strings in this case), but the Set can only contain each object once. In…
Pascal Klein
  • 23,665
  • 24
  • 82
  • 119
17
votes
2 answers

SPLFileInfo: get filename without extension

I'm accessing a number of files in the SPLFileInfo object. I see a way to get the path, filename, and even extension of the file. Is there a way to get the filename without extension? Here's the code I've been working with but I'm hoping to get…
tzvi
  • 471
  • 3
  • 5
  • 19
17
votes
3 answers

SplFileObject vs fopen in PHP

What are the pros and cons of using fopen as opposed to SplFileObject in PHP? From what I see, SplFileObject throws exceptions where applicable which makes this convenient when using try...catch for error handling. Apart from this, are there any…
Agnel Kurian
  • 57,975
  • 43
  • 146
  • 217
16
votes
7 answers

How do I alter array keys and values while using a RecursiveArrayIterator?

I suspect I'm doing something stupid here, but I'm confused by what seems like a simple problem with SPL: How do I modified the contents of an array (the values in this example), using a RecursiveArrayIterator / RecursiveIteratorIterator? Using the…
John Carter
  • 53,924
  • 26
  • 111
  • 144
16
votes
3 answers

Does really SplFixedArray perform better than arrays?

I'm testing the SplFixedArray building an array with the days of the week, and I get the following results:
Isra
  • 602
  • 1
  • 6
  • 14
15
votes
2 answers

PHP autoloader class vs. procedural autoloader function?

Up to this point I've used procedural standalone autoloader functions and registered them with spl_autoload_register() to automatically load my (usually) namespaced classes. Lately, though, I've noticed people mentioning the use of autoloader…
user895378
15
votes
8 answers

PHP lazy array mapping

Is there a way of doing array_map but as an iterator? For example: foreach (new MapIterator($array, $function) as $value) { if ($value == $required) break; } The reason to do this is that $function is hard to calculate and $array has too…
Ezequiel
  • 668
  • 2
  • 9
  • 25
15
votes
4 answers

Sort directory listing using RecursiveDirectoryIterator

I'm using RecursiveDirectoryIterator and RecursiveIteratorIterator to build a file listing tree using code like below. I need to the list to be sorted - either directories then files alphabetically, or just alphabetically. Can anyone tell me how to…
Scott Saunders
  • 29,840
  • 14
  • 57
  • 64
15
votes
1 answer

PHP array_key_exists() and SPL ArrayAccess interface: not compatible?

I wrote a simple collection class so that I can store my arrays in objects: class App_Collection implements ArrayAccess, IteratorAggregate, Countable { public $data = array(); public function count() { return…
Aron Rotteveel
  • 81,193
  • 17
  • 104
  • 128
14
votes
1 answer

How to close a SplFileObject file handler?

I'm working with files in PHP using SplFileInfo and SplFileObject. But when I try to "re-open" a file, it yells me : SplFileObject::__construct(filemame): failed to open stream: Permission denied I think I should close my file before re-opening…
Gilles
  • 317
  • 5
  • 15
13
votes
1 answer

PHP Recursive Iterator: Parent key of current array iteration?

I have an array like this: $arr = array( $foo = array( 'donuts' => array( 'name' => 'lionel ritchie', 'animal' => 'manatee', ) ) ); Using that magic of the 'SPL…
Captain flapjack
  • 426
  • 6
  • 16
13
votes
1 answer

What is the difference between SplObjectStorage::contains and SplObjectStorage::offsetExists?

The PHP documentation is not very explicit and only states that: SplObjectStorage::offsetExists Checks whether an object exists in the storage. (PHP >= 5.3.0) SplObjectStorage::contains Checks if the storage contains the object provided. (PHP >=…
Tivie
  • 18,864
  • 5
  • 58
  • 77
1
2
3
24 25