new spl features in php 5.3
DESCRIPTION
http://matthewturland.com/2010/05/20/new-spl-features-in-php-5-3/TRANSCRIPT
![Page 1: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/1.jpg)
New SPL Features in PHP 5.3
Matthew Turland
TEK-X
May 20, 2010
![Page 2: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/2.jpg)
Hi! My name is…
• Senior Platform Engineer for Synacor, Inc.
• Former author and TE for php|architect
• Author of Web Scraping with PHP
• Past contributor to Zend Framework
• Lead developer of Phergie
• ULL alumni with a BS in computer science
![Page 3: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/3.jpg)
And I work for…
• Provides internet solutions to ISPs, media companies, and advertisers
• International company with offices in Buffalo, New York City, Los Angeles, and Amsterdam
• Clientele includes most of the top 20 cable providers in the United States
• Great company – join us!
![Page 4: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/4.jpg)
What about you?
• Used the SPL before?
• Using PHP 5.3?
• Computer science background?
• Knowledge of data structures?
![Page 5: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/5.jpg)
Pre-5.3 SPL Features
• Classes: ArrayObject, SplFileInfo, SplSubject, SplObserver, etc.
• Iterators: RecursiveIteratorIterator, FilterIterator, LimitIterator, etc.
• Interfaces: ArrayAccess, Countable, Iterator, IteratorAggregate, etc.
• Functions: spl_autoload_register, iterator_to_array, spl_object_hash, etc.
![Page 6: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/6.jpg)
Containers
“A container is a class, a data structure, or
an abstract data type whose instances are
collections of other objects. They are used
to store objects in an organized way
following specific access rules.”
Container (data structure) - Wikipedia
![Page 7: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/7.jpg)
Why containers?
• We already have arrays and strings!
array() 'string'
![Page 8: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/8.jpg)
Two excellent reasons
• Versus traditional arrays, there is potential for:
– Less CPU usage
– Less memory usage
![Page 9: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/9.jpg)
Arrays are (not always) great
• Flexible general purpose container
• Underlying hash table algorithm is not always ideal for the task at hand
![Page 10: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/10.jpg)
Warning: Benchmarks Ahead
• Lies, Outrageous Lies, and Benchmarks
• PHP 5.3.2 compiled on Ubuntu 9.10
• Intel Core2Duo 1.83 GHz, 4 GB DDR2 RAM
• Performance results are shown in executions per second rather than time per execution to avoid really small numbers
• Code and results
![Page 11: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/11.jpg)
The List
![Page 12: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/12.jpg)
SplFixedArray - What
• Like an array, but with a fixed length
• Only allows integers >= 0 for keys
• Can be resized, but at a cost
• Not compatible with array functions
![Page 13: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/13.jpg)
SplFixedArray - When
• It’s best to use this when:
– You know in advance how many elements you want to store (e.g. mysql_num_rows)
– You only need to access elements in sequential order
![Page 14: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/14.jpg)
SplFixedArray - Code
<?php
$a = array();
for ($i = 0; $i < $argv[1]; $i++) {
$a[$i] = $i;
$i = $a[$i];
}
<?php
$a = new SplFixedArray($argv[1]);
for ($i = 0; $i < $argv[1]; $i++) {
$a[$i] = $i;
$i = $a[$i];
}
![Page 15: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/15.jpg)
SplFixedArray - EPS
![Page 16: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/16.jpg)
SplFixedArray - Memory
![Page 17: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/17.jpg)
SplDoublyLinkedList - What
• Mainly intended as a parent class
• Same unlimited size as arrays without the associated hash map algorithm
• Less performance, but more memory efficiency
![Page 18: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/18.jpg)
SplDoublyLinkedList - When
• It’s best to use this when:
– You do not know in advance how many elements you want to store
– You only need to access elements in sequential order
![Page 19: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/19.jpg)
SplDoublyLinkedList - Code
<?php
$a = array();
for ($i = 0; $i < $argv[1]; $i++) {
$a[] = $i;
$i = $a[$i];
}
<?php
$a = new SplDoublyLinkedList($argv[1]);
for ($i = 0; $i < $argv[1]; $i++) {
$a[] = $i;
$i = $a[$i];
}
![Page 20: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/20.jpg)
SplDoublyLinkedList - EPS
![Page 21: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/21.jpg)
SplDoublyLinkedList - Memory
![Page 22: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/22.jpg)
The Stack
![Page 23: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/23.jpg)
SplStack - What
• 2 operations
– Push: [] for both array and SplStack
– Pop: array_pop() vs SplStack::pop()
• Last In, First Out (LIFO)
– The last item pushed onto the top of the stack is the first item that will be popped off of the top of the stack
![Page 24: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/24.jpg)
SplStack - When
• It’s best to use this when:
– You do not know in advance how many elements you want to store
– You only ever need to access the last element you stored
![Page 25: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/25.jpg)
SplStack - Code
<?php
$a = array();
for($i = 0; $i < $argv[1]; $i++) {
$a[] = $i;
}
for($i = 0; $i < $argv[1]; $i++) {
array_pop($a);
}
![Page 26: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/26.jpg)
SplStack - Code (cont.)
<?php
$a = new SplStack;
for($i = 0; $i < $argv[1]; $i++) {
$a[] = $i;
}
for($i = 0; $i < $argv[1]; $i++) {
$a->pop();
}
![Page 27: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/27.jpg)
SplStack - EPS
![Page 28: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/28.jpg)
SplStack - Memory
![Page 29: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/29.jpg)
The Queue
![Page 30: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/30.jpg)
SplQueue - What
• 2 operations
– Enqueue: [] for both array and SplQueue
– Dequeue: array_shift() vs SplQueue::dequeue()
• First In, First Out (FIFO)
– The first item added to the end of the queue will be the first item removed from the front of the queue
![Page 31: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/31.jpg)
SplQueue - When
• It’s best to use this when:
– You do not know in advance how many elements you want to store
– You only ever need to access the remaining element that was stored earliest
![Page 32: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/32.jpg)
SplQueue - Code
<?php
$a = array();
for($i = 0; $i < $argv[1]; $i++) {
$a[] = $i;
}
for($i = 0; $i < $argv[1]; $i++) {
array_shift($a);
}
![Page 33: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/33.jpg)
SplQueue - Code (cont.)
<?php
$a = new SplQueue;
for($i = 0; $i < $argv[1]; $i++) {
$a[] = $i;
}
for($i = 0; $i < $argv[1]; $i++) {
$a->dequeue();
}
![Page 34: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/34.jpg)
SplQueue - EPS
![Page 35: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/35.jpg)
SplQueue - Memory
![Page 36: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/36.jpg)
The Heap
![Page 37: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/37.jpg)
SplHeap - What
• 2 operations
– Insert: [] + sort vs SplHeap::insert()
– Remove: array_shift() vs SplHeap::extract()
• Internally reorders items based on comparison
– SplHeap::compare() can be overridden
• Subclasses
– SplMinHeap
– SplMaxHeap
• Better worst-case scenario performance versus arrays (heap sort versus quick sort)
![Page 38: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/38.jpg)
SplHeap - When
• It’s best to use this when:
– You do not know in advance how many elements you want to store
– You need to access elements in an order based on how they compare to each other
![Page 39: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/39.jpg)
SplMinHeap - Code
<?php
$a = array();
for($i = 0; $i < $argv[1]; $i++) {
$a[] = rand(1, $argv[1]);
sort($a);
}
for($i = 0; $i < $argv[1]; $i++) {
array_shift($a);
}
![Page 40: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/40.jpg)
SplMinHeap - Code (cont.)
<?php
$a = new SplMinHeap;
for($i = 0; $i < $argv[1]; $i++) {
$a->insert(rand(1, $argv[1]));
}
for($i = 0; $i < $argv[1]; $i++) {
$a->extract();
}
![Page 41: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/41.jpg)
SplMinHeap - EPS
![Page 42: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/42.jpg)
SplMinHeap - Memory
![Page 43: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/43.jpg)
The Priority Queue
![Page 44: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/44.jpg)
SplPriorityQueue - What
• Accepts a priority with the element value
• Element with highest priority comes out first
• Priorities may be of any comparable type
• SplPriorityQueue::compare() can be overridden
• Operates similarly to a heap
– In fact, it uses a heap internally for storage
![Page 45: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/45.jpg)
SplPriorityQueue - When
• It’s best to use this when:
– You do not know in advance how many elements you want to store
– You need to access elements in an order based on how priority values associated with those elements compare to each other
![Page 46: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/46.jpg)
SplPriorityQueue - Code<?php
$threshold = $argv[1] * 0.1;
$a = array();
$i = 0;
do {
if ($i <= $argv[1]) {
$a[] = array($i, rand(1, 10));
usort($a, 'priority_sort');
}
if ($i > $threshold) {
array_shift($a);
}
$i++;
} while (count($a));
function priority_sort($a, $b) {
return $a[1] - $b[1];
}
![Page 47: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/47.jpg)
SplPriorityQueue - Code (cont.)<?php
$threshold = $argv[1] * 0.1;
$a = new SplPriorityQueue;
$i = 0;
do {
if ($i < 1) {
$a->insert($i, rand(1,10));
}
if ($i > $threshold) {
$a->extract();
}
$i++;
} while (count($a));
![Page 48: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/48.jpg)
SplPriorityQueue - EPS
![Page 49: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/49.jpg)
SplPriorityQueue - Memory
![Page 50: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/50.jpg)
The Set
![Page 51: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/51.jpg)
The Composite Hash Map
Just pretend the red pills are objects.
![Page 52: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/52.jpg)
SplObjectStorage - What
• Combination of two data structures
– Composite hash map: a hash map with objects for keys; the spl_object_hash() function must be used for arrays to have this capability
– Set: focuses on a group of values rather than individual values with operations like union, intersection, difference, and element_of; no concept of sequential order
• Currently lacks a method for the intersection operation
![Page 53: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/53.jpg)
SplObjectStorage - When
• It’s best to use this when:
– You need to store data using composite (i.e. non-scalar) keys
– You need the ability to access data using set operations more so than accessing it in a particular order
![Page 54: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/54.jpg)
SplObjectStorage - Code<?php
$a = array();
for ($i = 0; $i < $argv[1]; $i++) {
$object = new stdClass;
$a[spl_object_hash($object)] = $object;
}
$a = array();
$b = array();
for ($i = 0; $i < $argv[1]; $i++) {
$a[] = rand(1, $argv[1]);
$b[] = rand(1, $argv[1]);
}
$c = array_merge($a, $b);
$c = array_diff($a, $b);
![Page 55: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/55.jpg)
SplObjectStorage - Code (cont.)<?php
$a = new SplObjectStorage;
for ($i = 0; $i < $argv[1]; $i++) {
$object = new stdClass;
$a->attach($object, $object);
}
$a = new SplObjectStorage;
$b = new SplObjectStorage;
for ($i = 0; $i < $argv[1]; $i++) {
$a->attach((object) rand(1, $argv[1]));
$b->attach((object) rand(1, $argv[1]));
}
$c = clone $a;
$c->addAll($b);
$c = clone $a;
$c->removeAll($b);
![Page 56: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/56.jpg)
SplObjectStorage - EPS
![Page 57: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/57.jpg)
SplObjectStorage - Memory
![Page 58: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/58.jpg)
Thank this guy
• Etienne Kneuss did a lot of the work on the new SPL features in PHP 5.3
![Page 59: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/59.jpg)
Some great SPL resources
• SPL in the PHP manual
• Etienne Kneuss' blog
• "SPL to the Rescue" by Elizabeth Smith
• “SPL, a bridge not too far” by Michelangelo van Dam
• This presentation as a blog post
![Page 60: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/60.jpg)
Possible future SPL features
• Graphs
– Contain nodes and edges connecting them
– Directional / non-directional
– Cyclic / acyclic
– Connected / unconnected
– Edge costs
• Trees
– Acyclic unidirectional graph
– Hierarchical in nature
![Page 62: New SPL Features in PHP 5.3](https://reader030.vdocuments.site/reader030/viewer/2022020207/549478c7ac79591d2e8b4b44/html5/thumbnails/62.jpg)
That’s all, folks
• Any questions?
• http://synacor.com
• http://matthewturland.com
• @elazar on Twitter
• Elazar on the Freenode IRC network