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

What are the benefits of using SPL ArrayObject, ArrayIterator, RecursiveArrayIterator instead of regular arrays?

I've started to learn PHP SPL from ArrayIterators and I'd like to know what are the benefits of using SPL ArrayObject, ArrayIterator, RecursiveArrayIterator instead of regular arrays? a) I've heard that loops using SPL iterators will reduce memory…
Kirzilla
  • 16,368
  • 26
  • 84
  • 129
10
votes
4 answers

How to get number of lines with SplFileObject?

$file = new SplFileObject('/path/to/file.txt'); How can I find the number of lines in a file with SplFileObject?
Richard Knop
  • 81,041
  • 149
  • 392
  • 552
10
votes
2 answers

How to get the object associated with the current key while iterating through SplObjectStorage in PHP 5.4

In PHP 5.4 I have an instance of SplObjectStorage where I associate objects with some extra metadata. I need to then iterate through the instance of SplObjectStorage and retrieve the object associated with the current key. I attempted to use…
Phillip Whelan
  • 1,697
  • 2
  • 17
  • 28
10
votes
4 answers

Is there any native PHP function which throws an built-in Exception?

While answering PHP rename() doesn't throws exception on error I was wondering if there are any native PHP functions which throw a built-in Exception, beside the SPL stuff?
powtac
  • 40,542
  • 28
  • 115
  • 170
9
votes
2 answers

Is there a way to make PHP's SplHeap recalculate? (aka: add up-heap to SplHeap?)

I am using an SplHeap to hold graph nodes of a tree with directed edges that will be traversed from the leaves to the root. For this, I precalculate the "fan-in" of nodes and put them into the heap so that I can always retrieve the node with the…
cidermole
  • 5,662
  • 1
  • 15
  • 21
8
votes
1 answer

Is it possible to use SplEnum in php 5.2.6?

I tried to use class : abstract class my_abstractEnum extends SplEnum { ... } and class my_categoryEnum extends my_abstractEnum { ... } and I have : Fatal error: Class 'SplEnum' not found I work on PHP 5.2.6. SplEnum is for php > 5.3 ? I don't…
Cédric
  • 145
  • 2
  • 4
8
votes
2 answers

PHP RecursiveIterator traversing

I have a structure representing a form and I want to iterate it using RecursiveIterator. The problem is this only returns the top-level questions. What am I doing wrong? Whole form: class Form implements RecursiveIterator{ private $id; …
cypher
  • 6,822
  • 4
  • 31
  • 48
8
votes
3 answers

Using spl_autoload() not able to load class

I'm playing around with the SPL autoload functionality and seem to be missing something important as I am currently unable to get it to work. Here is the snippet I am currently using: // ROOT_DIRECTORY translates to…
John Conde
  • 217,595
  • 99
  • 455
  • 496
8
votes
4 answers

SPL vs. Array: When should we use SPL and when should we use Array in PHP?

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…
Ben
  • 25,389
  • 34
  • 109
  • 165
8
votes
0 answers

Get a stream resource from a SplFileInfo object

I'm calling a method that expects a stream resource for a file, like this: $obj->method(fopen($splFileInfo, 'r+')); The $splFileInfo object is an instance of SplFileInfo. Is there a way to get the stream resource of the $splFileInfo object without…
Daniel Ribeiro
  • 10,156
  • 12
  • 47
  • 79
8
votes
3 answers

Using RecursiveIteratorIterator to list files in folder is great. Can I get the parent path and full path as well?

I need to list all files (which have certain extensions) in a folder and its sub-folders. I used RecursiveIteratorIterator as described in @Matthew 's great answer in PHP list all files in directory. I'm setting the root of the search to ".." and…
user1639431
  • 3,693
  • 8
  • 24
  • 22
8
votes
2 answers

Sorting files with DirectoryIterator

I'm making a directory listing PHP5 script for lighttpd. In a given directory, I'd like to be able to list direct sub-directories and files (with informations). After a quick search, DirectoryIterator seems to be my friend: foreach (new…
abernier
  • 27,030
  • 20
  • 83
  • 114
8
votes
2 answers

If PHP libraries can register their own autoloaders, then why does PSR-0 require they be in uniform directories?

I'm building a framework (this is a vast simplification -- please don't recommend using an existing framework instead, that's not helpful) into which I would like to be able to integrate other libraries. The PSR-0 recommendation suggests that all…
ringmaster
  • 2,879
  • 3
  • 28
  • 31
7
votes
3 answers

Extending ArrayObject in PHP properly?

Problem: I am trying to extend PHP's ArrayObject as shown below. Unfortunately I can't get it to work properly when setting multi-dimensional objects and instead an error thrown as I have the strict settings enabled in PHP. (Error: Strict standards:…
Industrial
  • 41,400
  • 69
  • 194
  • 289
7
votes
5 answers

PHP how to array_unshift on an arrayObject

As stated in the title, How do you perform an array_unshift() on a arrayObject, array_push() is obtained by doing an arrayObject->append() but what about unshift ? Edit: What I've forgot to mention is that i also need in this particular case to…
malko
  • 2,292
  • 18
  • 26
1 2
3
24 25