<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>Matthew Turland &#187; PHP</title> <atom:link href="http://matthewturland.com/tag/php/feed/" rel="self" type="application/rss+xml" /><link>http://matthewturland.com</link> <description></description> <lastBuildDate>Tue, 15 May 2012 02:29:07 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.2</generator> <item><title>New SPL Features in PHP 5.3</title><link>http://matthewturland.com/2010/05/20/new-spl-features-in-php-5-3/</link> <comments>http://matthewturland.com/2010/05/20/new-spl-features-in-php-5-3/#comments</comments> <pubDate>Thu, 20 May 2010 15:00:53 +0000</pubDate> <dc:creator>Matthew Turland</dc:creator> <category><![CDATA[PHP]]></category> <category><![CDATA[SPL]]></category><guid
isPermaLink="false">http://matthewturland.com/?p=107</guid> <description><![CDATA[Note: I&#8217;ve written on this topic before, but thought the subject warranted further more detailed discussion and a more comprehensive and up-to-date set of benchmarks. Hence, this post and this presentation. Enjoy. Update: This post and the benchmarks have been updated for PHP 5.3.4 and Ubuntu 10.10. (12/14/2010) The SPL, or Standard PHP Library, is [...]]]></description> <content:encoded><![CDATA[<p><em>Note: I&#8217;ve <a
title="The SPL Deserves Some Reiteration | Blue Parabola, LLC" href="http://blueparabola.com/blog/spl-deserves-some-reiteration">written on this topic before</a>, but thought the subject warranted further more detailed discussion and a more comprehensive and up-to-date set of benchmarks. Hence, this post and <a
title="New SPL Features in PHP 5.3" href="http://www.slideshare.net/tobias382/new-spl-features-in-php-53">this presentation</a>. Enjoy.</em></p><p><strong>Update: This post and the benchmarks have been updated for PHP 5.3.4 and Ubuntu 10.10. (12/14/2010)</strong></p><p>The <a
title="PHP: SPL - Manual" href="http://us3.php.net/spl">SPL</a>, or Standard PHP Library, is an often overlooked extension in the PHP core. It first came on the scene in PHP 5 and a variety of <a
title="PHP: Iterators - Manual" href="http://php.net/manual/en/spl.iterators.php">iterators</a> constituted the majority of its initial offerings. Though the <a
title="PHP: New Classes - Manual" href="http://www.php.net/manual/en/migration53.classes.php">iterator offerings were expanded in PHP 5.3</a>, the particularly interesting additions to the SPL were several specialized <a
title="Data structure - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/Data_structures">data structure</a> <a
title="PHP: Datastructures - Manual" href="http://php.net/manual/en/spl.datastructures.php">classes</a>, the foundational concepts for which originate in the field of <a
title="Computer science - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/Computer_science">computer science</a>. In this post, I will provide an overview of these new classes and explain why and when they should be used.</p><h3>Arrays</h3><p>While PHP has several data types, the ones that likely see the most frequent and varied use are <a
title="PHP: Strings - Manual" href="http://php.net/manual/en/language.types.string.php">strings</a> and <a
title="PHP: Arrays - Manual" href="http://php.net/manual/en/language.types.array.php">arrays</a>. They are the proverbial <a
title="Duct tape - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/Duct_tape#Common_uses">duct tape</a> and <a
title="WD-40 - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/WD-40#Function">WD-40</a> of PHP, respectively. Like arrays, SPL data structure classes are used to store composite (i.e. non-<a
title="Scalar (computing) - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/Scalar_(computing)">scalar</a>) data.</p><p>Now, that&#8217;s not to say that every instance of an array in existing codebases should be replaced with an SPL container object. There are cases where it&#8217;s appropriate to use one over the other. Knowing the difference requires an understanding of how arrays work.</p><p>Within the C code that makes up the PHP interpreter, arrays are implemented as a data structure called a <a
title="Hash table - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/Hash_table">hash table or hash map</a>. When a value contained within an array is referenced by its index, PHP uses a <a
title="[svn] Contents of /php/php-src/trunk/Zend/zend_hash.h" href="http://svn.php.net/viewvc/php/php-src/trunk/Zend/zend_hash.h?revision=298204&amp;view=markup#l228">hashing function</a> to convert that index into a unique hash representing the location of the corresponding value within the array.</p><p>This hash map implementation enables arrays to store an arbitrary number of elements and provide access to all of those elements simultaneously using either numeric or string keys. Arrays are extremely fast for the capabilities they provide and are an excellent general purpose data structure.</p><h3>Fixed Arrays</h3><p>In contrast to arrays, <a
title="PHP: SplFixedArray - Manual" href="http://php.net/manual/en/class.splfixedarray.php"><code>SplFixedArray</code></a> functions more like <a
title="Arrays" href="http://www.cplusplus.com/doc/tutorial/arrays/">C arrays</a> or <a
title="Arrays (The Java™ Tutorials &gt; Learning the Java Language &gt; Language Basics)" href="http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html">Java arrays</a> than <a
title="PHP: Arrays - Manual" href="http://php.net/manual/en/language.types.array.php">PHP arrays</a>. The maximum number of elements that it may contain is specified upon instantiation. While it is possible to change it later via the <code>setSize()</code> method, this negates the performance advantages of using it: because its size is fixed, it doesn&#8217;t need to use a hashing function to resolve the position of elements within the array. <strong>It makes sense to use fixed arrays when the number of elements to be stored is known in advance and the elements only need to be accessed by sequential position.</strong></p><p><code>SplFixedArray</code> implements the <a
title="SPL-StandardPHPLibrary: Iterator Interface Reference" href="http://www.php.net/~helly/php/ext/spl/interfaceIterator.html"><code>Iterator</code></a>, <a
title="SPL-StandardPHPLibrary: ArrayAccess Interface Reference" href="http://www.php.net/~helly/php/ext/spl/interfaceArrayAccess.html"><code>ArrayAccess</code></a>, and <a
title="SPL-StandardPHPLibrary: Countable Interface Reference" href="http://www.php.net/~helly/php/ext/spl/interfaceCountable.html"><code>Countable</code></a> interfaces. <code>Iterator</code> allows it to be iterated using a <a
title="PHP: foreach - Manual" href="http://php.net/manual/en/control-structures.foreach.php"><code>foreach</code></a> loop. <code>ArrayAccess</code> provides access to its elements using <a
title="PHP: Arrays - Manual" href="http://php.net/manual/en/language.types.array.php#language.types.array.syntax.modifying">array syntax</a> where elements are referred to using integer positions beginning at 0 as with enumerated arrays. <code>Countable</code> enables a list to be passed to the <a
title="PHP: count - Manual" href="http://us3.php.net/count"><code>count()</code></a> function like an array.</p><p>Aside from the inability to use it in place of arrays with <a
title="PHP: Array Functions - Manual" href="http://php.net/manual/en/ref.array.php">array functions</a>, instances of <code>SplFixedArray</code> function just like arrays for all intents and purposes. It&#8217;s even possible to convert them to and from arrays using the <a
title="PHP: SplFixedArray::toArray - Manual" href="http://php.net/manual/en/splfixedarray.toarray.php"><code>toArray()</code></a> and <a
title="PHP: SplFixedArray::fromArray - Manual" href="http://php.net/manual/en/splfixedarray.fromarray.php"><code>fromArray()</code></a> methods respectively. However, it generally makes more sense to use <code>SplFixedArray</code> exclusively for each individual use case.</p><h3>Lists</h3><p>In <a
title="Computer science - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/Computer_science">computer science</a>, a <a
title="List (computing) - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/List_(computing)">list</a> is defined as an ordered collection of values. A <a
title="Linked list - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/Linked_list">linked list</a> is a data structure in which each element in the list includes a reference to one or both of the elements on either side of it within the list. The term &#8220;<a
title="Doubly-linked list - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/Doubly-linked_list">doubly-linked list</a>&#8221; is used to refer to the latter case. In the SPL, this takes the form of the class <a
title="PHP: SplDoublyLinkedList - Manual" href="http://php.net/manual/en/class.spldoublylinkedlist.php"><code>SplDoublyLinkedList</code></a>.</p><p>Like <code>SplFixedArray</code>, <code>SplDoublyLinkedList</code> also implements the <a
title="SPL-StandardPHPLibrary: Iterator Interface Reference" href="http://www.php.net/~helly/php/ext/spl/interfaceIterator.html"><code>Iterator</code></a>, <a
title="SPL-StandardPHPLibrary: ArrayAccess Interface Reference" href="http://www.php.net/~helly/php/ext/spl/interfaceArrayAccess.html"><code>ArrayAccess</code></a>, and <a
title="SPL-StandardPHPLibrary: Countable Interface Reference" href="http://www.php.net/~helly/php/ext/spl/interfaceCountable.html"><code>Countable</code></a> interfaces. In addition to the methods that come with these interface implementations, elements can be added to or removed from the start or end of the list using its <a
title="PHP: SplDoublyLinkedList::push - Manual" href="http://php.net/manual/en/spldoublylinkedlist.push.php"><code>push()</code></a>, <a
title="PHP: SplDoublyLinkedList::pop - Manual" href="http://php.net/manual/en/spldoublylinkedlist.pop.php"><code>pop()</code></a>, <a
title="PHP: SplDoublyLinkedList::shift - Manual" href="http://php.net/manual/en/spldoublylinkedlist.shift.php"><code>shift()</code></a> and <a
title="PHP: SplDoublyLinkedList::unshift - Manual" href="http://php.net/manual/en/spldoublylinkedlist.unshift.php"><code>unshift()</code></a> methods, which correspond to the <a
title="PHP: array_push - Manual" href="http://us3.php.net/array_push"><code>array_push()</code></a>, <a
title="PHP: array_pop - Manual" href="http://us2.php.net/array_pop"><code>array_pop()</code></a>, <a
title="PHP: array_shift - Manual" href="http://us3.php.net/array_shift"><code>array_shift()</code></a>, and <a
title="PHP: array_unshift - Manual" href="http://us2.php.net/array_unshift"><code>array_unshift()</code></a> functions respectively. Unfortunately, as of PHP 5.3.4, there&#8217;s no way to insert an element anywhere in the list other than at the beginning or the end. A <a
title="PHP Bugs: #48358: SplDoublyLinkedList needs an insertAfterIterator() method or something similar" href="https://bugs.php.net/bug.php?id=48358">feature request</a> has been filed for this. Add a comment or vote to show support for its addition.</p><p>The elements at the start and end of the list are accessible via its <a
title="PHP: SplDoublyLinkedList::top - Manual" href="http://php.net/manual/en/spldoublylinkedlist.top.php"><code>top()</code></a> and <a
title="PHP: SplDoublyLinkedList::bottom - Manual" href="http://php.net/manual/en/spldoublylinkedlist.bottom.php"><code>bottom()</code></a> methods respectively, which correspond to the <a
title="PHP: reset - Manual" href="http://php.net/manual/en/function.reset.php"><code>reset()</code></a> and <a
title="PHP: end - Manual" href="http://php.net/manual/en/function.end.php"><code>end()</code></a> functions. Like <code>SplFixedArray</code>, elements can also be accessed arbitrarily by positional index using the array syntax granted by <code>ArrayAccess</code>. <strong>It makes sense to use lists when the number of elements to be stored is not known in advance and the elements only need to be accessed by sequential position.</strong></p><h3>Stacks</h3><p><a
title="Stack (data structure) - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/Stack_(data_structure)">Stacks</a> are similar to lists with two major differences. First, elements can only be added to the top of the stack. Second, an element can only be accessed by taking it off the top of the stack. Because of these differences, the stack is often referred to as a Last-In-First-Out or <a
title="LIFO (computing) - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/LIFO_(computing)">LIFO</a> data structure. <a
title="PHP: SplStack - Manual" href="http://php.net/manual/en/class.splstack.php"><code>SplStack</code></a> is the SPL stack implementation.</p><p><code>SplStack</code> is a bit removed from the traditional definition of a stack. It extends <code>SplDoublyLinkedList</code> and inherits its abilities, some of which don&#8217;t really apply to stacks. In order to enforce its restriction on how elements are accessed, <code>SplStack</code> overrides the <a
title="PHP: SplDoublyLinkedList::setIteratorMode - Manual" href="http://php.net/manual/en/spldoublylinkedlist.setiteratormode.php"><code>setIteratorMode()</code></a> method of its parent class and implements <a
title="PHP: SplStack::setIteratorMode - Manual" href="http://php.net/manual/en/splstack.setiteratormode.php">its own</a> to prevent modification of the iteration direction. Both methods allow elements to be retained or removed as they are iterated.</p><p><strong>Use of stacks makes sense when the number of elements to be stored is not known in advance and the only element that must be accessible is the last one stored.</strong> However, as of PHP 5.3.4, the performance of <code>SplStack</code> leaves something to be desired. Benchmarks included later in this provide an objective illustration of this, though the cause of the behavior remains unknown.</p><h3>Queues</h3><p><a
title="Queue (data structure) - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/Queue_(data_structure)">Queues</a> are also similar to lists, again with two major differences. First, elements can only be added (or &#8220;enqueued&#8221;) to the end of the queue. Second, an element can only be accessed by removing (or &#8220;dequeueing&#8221;) it from the beginning of the queue. For these reasons the queue is referred to as a First-In-First-Out or <a
title="FIFO - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/FIFO">FIFO</a> data structure. The <a
title="PHP: SplQueue - Manual" href="http://php.net/manual/en/class.splqueue.php"><code>SplQueue</code></a> class implements this data structure in the SPL.</p><p><code>SplQueue</code> follows suit with <code>SplStack</code> in extending <code>SplDoublyLinkedList</code>. Just as <code>SplStack</code> resultingly inherits some operations with at least questionable applicability, so too does <code>SplQueue</code>. Likewise, it overrides <code>setIteratorMode()</code> with <a
title="PHP: SplQueue::setIteratorMode - Manual" href="http://php.net/manual/en/splqueue.setiteratormode.php">its own version</a> to restrict how elements are accessed. <strong>Use of queues makes sense when the number of elements to be stored is not known in advance and the only element that must be accessible is the remaining element that was stored earliest.</strong></p><p>One minor difference between <code>SplQueue</code> and <code>SplStack</code> is that the former contains two method aliases named after conceptual queue operations: <a
title="PHP: SplQueue::dequeue - Manual" href="http://php.net/manual/en/splqueue.dequeue.php"><code>dequeue()</code></a> aliases <a
title="PHP: SplDoublyLinkedList::shift - Manual" href="http://php.net/manual/en/spldoublylinkedlist.shift.php"><code>SplDoublyLinkedList::shift()</code></a> and <a
title="PHP: SplQueue::enqueue - Manual" href="http://php.net/manual/en/splqueue.enqueue.php"><code>enqueue()</code></a> aliases <a
title="PHP: SplDoublyLinkedList::push - Manual" href="http://php.net/manual/en/spldoublylinkedlist.push.php"><code>SplDoublyLinkedList::push()</code></a>. This makes sense because while <code>push()</code> and <code>pop()</code> share similar applicability to conceptual stack operations, they are already present in its parent class.</p><p>Despite their common ancestry, <code>SplQueue</code> appears to have better performance than <code>SplStack</code> as of PHP 5.3.4. Benchmarks included later in this post review this in more detail.</p><h3>Heaps</h3><p>Up to this point, the data structures discussed have resembled lists insofar as they contain elements in the order in which they were added. By contrast, when an element is added to a <a
title="Heap (data structure) - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/Heap_(data_structure)">heap</a>, a comparison function is used to compare the new element to other elements already in the heap and element is placed appropriately within the heap based on that function&#8217;s return value. The beauty of heaps is that their underlying algorithm does this with minimal element comparisons, so it&#8217;s extremely efficient. <strong>Using heaps makes sense when the number of elements to be stored is not known in advance and elements must be accessed in an order based on how they compare to each other.</strong></p><p><a
title="PHP: SplHeap - Manual" href="http://php.net/manual/en/class.splheap.php"><code>SplHeap</code></a> is an abstract class used to create a heap by extending it and providing a comparison function in the form of its <a
title="PHP: SplHeap::compare - Manual" href="http://php.net/manual/en/splheap.compare.php"><code>compare()</code></a> method. Only the root element of a heap, the one yielding the highest comparison function return value, may be accessed or removed from the heap at any given time. This is done using the <a
title="PHP: SplHeap::extract - Manual" href="http://php.net/manual/en/splheap.extract.php"><code>extract()</code></a> method of <code>SplHeap</code>. <code>SplHeap</code> implements the <a
title="SPL-StandardPHPLibrary: Iterator Interface Reference" href="http://www.php.net/~helly/php/ext/spl/interfaceIterator.html"><code>Iterator</code></a> and <a
title="SPL-StandardPHPLibrary: Countable Interface Reference" href="http://www.php.net/~helly/php/ext/spl/interfaceCountable.html"><code>Countable</code></a> interfaces but, because only the root element can be extracted, it does not implement the <a
title="SPL-StandardPHPLibrary: ArrayAccess Interface Reference" href="http://www.php.net/~helly/php/ext/spl/interfaceArrayAccess.html"><code>ArrayAccess</code></a> interface like the previously discussed data structure classes.</p><p>In addition to the abstract <code>SplHeap</code> class, two concrete implementations are also included in the SPL, namely <a
title="PHP: SplMinHeap - Manual" href="http://php.net/manual/en/class.splminheap.php"><code>SplMinHeap</code></a> and <a
title="PHP: SplMaxHeap - Manual" href="http://php.net/manual/en/class.splmaxheap.php"><code>SplMaxHeap</code></a>. The <a
title="PHP: SplMinHeap::compare - Manual" href="http://php.net/manual/en/splminheap.compare.php"><code>compare()</code></a> method of <code>SplMinHeap</code> returns a value such that the smallest element in the heap is the root element. Likewise, the <a
title="PHP: SplMaxHeap::compare - Manual" href="http://php.net/manual/en/splmaxheap.compare.php"><code>compare()</code></a> method of <code>SplMaxHeap</code> returns a value such that the largest element in the heap is the root element.</p><p>At first glance, using a subclass of <code>SplHeap</code> may seem equivalent to calling <a
title="PHP: sort - Manual" href="http://us.php.net/sort"><code>sort()</code></a> or a similar function on an array and accessing the elements in sequence. This is indeed the case if all elements are added to the array prior to it being sorted. However, situations such as elements arriving over time or inadequate memory to store all elements simultaneously may preclude this approach. Use of arrays in such situations would require repeated resorting of the entire array as new elements are added, which is inefficient. This is why using the corresponding heap class makes a lot more sense in that situation than repeated calls to <a
title="PHP: sort - Manual" href="http://us.php.net/sort"><code>sort()</code></a>, <a
title="PHP: min - Manual" href="http://php.net/manual/en/function.min.php"><code>min()</code></a> or <a
title="PHP: max - Manual" href="http://php.net/manual/en/function.max.php"><code>max()</code></a>. Additionally, <code>SplHeap</code> can be used to implement the <a
title="Heapsort - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/Heapsort">heapsort algorithm</a>, which has <a
title="Heapsort - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/Heapsort#Comparison_with_other_sorts">better worst case performance</a> than the <a
title="Quicksort - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/Quicksort">quicksort algorithm</a> <a
title="[svn] Contents of /php/php-src/trunk/Zend/zend_qsort.c" href="http://svn.php.net/viewvc/php/php-src/trunk/Zend/zend_qsort.c?revision=296679&amp;view=markup#l56">implementation</a> <a
title="[svn] Contents of /php/php-src/trunk/ext/standard/array.c" href="http://svn.php.net/viewvc/php/php-src/trunk/ext/standard/array.c?revision=298204&amp;view=markup#l541">used by arrays</a>.</p><h3>Priority Queues</h3><p><a
title="Priority queue - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/Priority_queue">Priority queues</a> are somewhat similar to heaps. In fact, while it doesn&#8217;t extend <code>SplHeap</code>, <a
title="PHP: SplPriorityQueue - Manual" href="http://php.net/manual/en/class.splpriorityqueue.php"><code>SplPriorityQueue</code></a> does make use of a heap structure internally to implement its functionality. The difference is that the <a
title="PHP: SplPriorityQueue::insert - Manual" href="http://www.php.net/manual/en/splpriorityqueue.insert.php"><code>insert()</code></a> method of <code>SplPriorityQueue</code> queue accepts both a value and an associated priority, removing the need to use an array or object to store both of these and define an appropriate comparison function in an <code>SplHeap</code> instance. Elements with the highest priority, like those in <code>SplMaxHeap</code> with the highest value, are the ones that come out first when <a
title="PHP: SplPriorityQueue::extract - Manual" href="http://php.net/manual/en/splpriorityqueue.extract.php"><code>extract()</code></a> is called. Note that elements with equal priority are returned in no particular order.</p><p>For reasons similar to those of <code>SplHeap</code>, <code>SplPriorityQueue</code> implements both <a
title="SPL-StandardPHPLibrary: Iterator Interface Reference" href="http://www.php.net/~helly/php/ext/spl/interfaceIterator.html"><code>Iterator</code></a> and <a
title="SPL-StandardPHPLibrary: Countable Interface Reference" href="http://www.php.net/~helly/php/ext/spl/interfaceCountable.html"><code>Countable</code></a> interfaces and does not implement the <a
title="SPL-StandardPHPLibrary: ArrayAccess Interface Reference" href="http://www.php.net/~helly/php/ext/spl/interfaceArrayAccess.html"><code>ArrayAccess</code></a> interface. Because it stores a value and priority per element, <code>SplPriorityQueue</code> includes a <a
title="PHP: SplPriorityQueue::setExtractFlags - Manual" href="http://php.net/manual/en/splpriorityqueue.setextractflags.php"><code>setExtractFlags()</code></a> method that modifies the behavior of <code>extract()</code> to return the stored value, the stored priority, or an array containing both. Priorities are not bound to a particular data type: strings, integers, or even composite data types can be used. <code>SplPriorityQueue</code> can be extended and its <a
title="PHP: SplPriorityQueue::compare - Manual" href="http://www.php.net/manual/en/splpriorityqueue.compare.php"><code>compare()</code></a> method overridden to customize the comparison logic.</p><p><strong>It makes sense to use a priority queue when the number of elements to be stored is not known in advance and elements must be accessed in an order based on how a value associated with each element (versus the element value itself) compares to the same associated values of other elements.</strong></p><h3>Sets and Composite Hash Maps</h3><p><a
title="PHP: SplObjectStorage - Manual" href="http://php.net/manual/en/class.splobjectstorage.php"><code>SplObjectStorage</code></a> combines some of the properties of two different data structures. First, it provides the same functionality of a <a
title="Hash table - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/Hash_table">hash table</a> that a normal array has, but without its associated inability to use objects as keys unless the <a
title="PHP: spl_object_hash - Manual" href="http://us.php.net/spl_object_hash"><code>spl_object_hash()</code></a> function is used. In other words, it implements a composite hash map. Second, it can be used as a <a
title="Set (computer science) - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/Set_(computer_science)">set</a> to store objects as data without a meaningful corresponding key or concept of sequential order.</p><p>Its <a
title="PHP: SplObjectStorage::attach - Manual" href="http://php.net/manual/en/splobjectstorage.attach.php"><code>attach()</code></a> method accepts an object key and the data to associate with it and its <a
title="PHP: SplObjectStorage::detach - Manual" href="http://php.net/manual/en/splobjectstorage.detach.php"><code>detach()</code></a> method allows data to be removed using its associated object key. To use the object as a set, simply exclude the <code>$data</code> parameter for <code>attach()</code> as it&#8217;s optional. The <a
title="Set (computer science) - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/Set_(computer_science)#Operations">set operations</a> implemented by <code>SplObjectStorage</code> all have array function counterparts. For example, the <a
title="PHP: SplObjectStorage::addAll - Manual" href="http://php.net/manual/en/splobjectstorage.addall.php"><code>addAll()</code></a> method and <a
title="PHP: array_merge - Manual" href="http://php.net/manual/en/function.array-merge.php"><code>array_merge()</code></a> function both correspond to the union set operation. The difference operation is available using the <a
title="PHP: SplObjectStorage::removeAll - Manual" href="http://php.net/manual/en/splobjectstorage.removeall.php"><code>removeAll()</code></a> method and <a
title="PHP: array_diff - Manual" href="http://php.net/manual/en/function.array-diff.php"><code>array_diff()</code></a> function and its variants. The <a
title="PHP: SplObjectStorage::contains - Manual" href="http://php.net/manual/en/splobjectstorage.contains.php"><code>contains()</code></a> method and <a
title="PHP: in_array - Manual" href="http://php.net/manual/en/function.in-array.php"><code>in_array()</code></a> function both implement the element_of operation. Sadly, only arrays have an implementation of the intersection operation in the form of <a
title="PHP: array_intersect - Manual" href="http://php.net/manual/en/function.array-intersect.php"><code>array_intersect()</code></a> and its variants. Tobias Schlitt has a <a
title="Python. Good, bad, evil -2-: Native sets - Blog - Open Source - schlitt.info" href="http://schlitt.info/opensource/blog/0722_python_good_bad_evil_02_native_sets.html">more in-depth analysis</a> of this data structure that includes implementations of the set operations lacking in the SPL itself.</p><p><strong>Update: A patch I&#8217;ve submitted has been <a
title="php.cvs: svn:_/php/php-src/_branches/PHP_5_3/ext/spl/spl_observer.c_branches/PHP_5_3/ext/spl/tests/SplObjectStorage_removeAllExcept_basic.phpt_branches/PHP_5_3/ext/spl/tests/SplObjectStorage_removeAllExcept_invalid_parameter.phpt_trunk/ext/spl/spl_observer.c_trunk/ext/spl/tests/SplObjectStorage_removeAllExcept_basic.phpt_trunk/ext/spl/tests/SplObjectStorage_removeAllExcept_invalid_parameter.phpt" href="http://news.php.net/php.cvs/64274">merged</a>. <a
title="PHP: SplObjectStorage::removeAllExcept - Manual" href="http://php.net/manual/en/splobjectstorage.removeallexcept.php">SplObjectStorage::removeAllExcept()</a>, which is equivalent to the set intersection operation, will become available in PHP 5.3.5. (1/5/2011)</strong></p><p>Like some of the other data structures in the SPL, <code>SplObjectStorage</code> implements the <a
title="SPL-StandardPHPLibrary: Iterator Interface Reference" href="http://www.php.net/~helly/php/ext/spl/interfaceIterator.html"><code>Iterator</code></a>, <a
title="SPL-StandardPHPLibrary: Countable Interface Reference" href="http://www.php.net/~helly/php/ext/spl/interfaceCountable.html"><code>Countable</code></a>, and <a
title="SPL-StandardPHPLibrary: ArrayAccess Interface Reference" href="http://www.php.net/~helly/php/ext/spl/interfaceArrayAccess.html"><code>ArrayAccess</code></a> interfaces. Oddly, it also implements the <a
title="PHP: Traversable - Manual" href="http://php.net/manual/en/class.traversable.php"><code>Traversable</code></a> interface (which is limited to internally defined classes and negates the need for implementation of the <code>Iterator</code> interface) and the <a
title="PHP: Serializable - Manual" href="http://php.net/manual/en/class.serializable.php"><code>Serializable</code></a> interface (and it is the only SPL data structure class to do so).</p><p><strong>Using this class makes sense when data must be stored using composite keys or the ability to access data using set operations is more important than accessing data in a specific order.</strong></p><h3>Benchmarks</h3><p><em>Standard disclaimer: There are <a
title="Lies, damned lies, and statistics - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/Lies,_damned_lies,_and_statistics">lies, damned lies, and benchmarks</a>. <a
title="your mileage may vary - Wiktionary" href="http://en.wiktionary.org/wiki/your_mileage_may_vary"><acronym
title="Your Mileage May Vary">YMMV</acronym></a>.</em></p><h4>Platform</h4><ul><li>System: <a
title="VGN-NR298E/S | VAIO® NR Series Notebook PC | Sony | SonyStyle USA" href="http://store.sony.com/webapp/wcs/stores/servlet/ProductDisplay?storeId=10151&amp;catalogId=10551&amp;langId=-1&amp;productId=8198552921665293693">Sony Vaio VGN-NR298E</a></li><li>CPU: <a
title="Intel® Core™2 Duo Processor T5550 (2M Cache, 1.83 GHz, 667 MHz FSB) with SPEC Code(s) SLA4E" href="http://ark.intel.com/products/32427/Intel-Core2-Duo-Processor-T5550-(2M-Cache-1_83-GHz-667-MHz-FSB)">Intel Core2Duo 1.83GHz</a></li><li>RAM: 4 GB DDR2</li><li>OS: <a
title="Ubuntu Home Page | Ubuntu" href="http://www.ubuntu.com/">Ubuntu</a> 10.10 Karmic Koala Desktop Edition 64-bit</li><li>PHP: Custom build of <a
title="PHP: Downloads" href="http://www.php.net/downloads.php#v5.3.2">5.3.4</a> (<a
title="Building PHP 5.3 packages on Ubuntu 9.04 (Jaunty) for Apache 2 | ZippyKid" href="https://zippykid.com/2009/08/24/building-php-5-3-packages-on-ubuntu-9-04-jaunty-for-apache-2/">here&#8217;s how to create one</a>) using this configuration: <code>--without-pear --without-sqlite --without-sqlite3 --without-pdo-sqlite</code></li></ul><h4>Process</h4><p>Code used is located in <a
title="elazar&#039;s spl-benchmarks at master - GitHub" href="https://github.com/elazar/spl-benchmarks">this GitHub repository</a>.</p><ol><li>Modify constant declarations at the top of runner.php as appropriate (50 executions per test were used to get the results below), then execute it from the command line. It will in turn execute each of the scripts in the tests directory, measuring execution time and memory usage. Results will be recorded in results/raw.csv.</li><li>To generate graphs, run graphs.php. This uses the <a
title="eZ Components - Documentation - Tutorials" href="http://ezcomponents.org/docs/tutorials/Graph">Graph component</a> from the <a
title="eZ Components" href="http://ezcomponents.org/">ezComponents library</a>. Resulting images will be written to the results directory in PNG format.</li></ol><h4>Results</h4><table><tbody><tr><td><a
title="SplFixedArray - Executions Per Second" href="https://github.com/elazar/spl-benchmarks/raw/master/results/splfixedarray_eps.png"><img
src="https://github.com/elazar/spl-benchmarks/raw/master/results/splfixedarray_eps.png" alt="SplFixedArray - Executions Per Second" width="400" height="225" /></a></td><td><a
title="SplFixedArray - Memory" href="https://github.com/elazar/spl-benchmarks/raw/master/results/splfixedarray_memory.png"><img
src="https://github.com/elazar/spl-benchmarks/raw/master/results/splfixedarray_memory.png" alt="SplFixedArray - Memory" width="400" height="225" /></a></td><td><strong>Code</strong><a
title="tests/splfixedarray-array.php at master from elazar&#039;s spl-benchmarks - GitHub" href="https://github.com/elazar/spl-benchmarks/blob/master/tests/splfixedarray-array.php">Array</a><a
title="tests/splfixedarray-spl.php at master from elazar&#039;s spl-benchmarks - GitHub" href="https://github.com/elazar/spl-benchmarks/blob/master/tests/splfixedarray-spl.php">SPL</a></td></tr><tr><td><a
title="SplDoublyLinkedList - Executions Per Second" href="https://github.com/elazar/spl-benchmarks/raw/master/results/spldoublylinkedlist_eps.png"><img
src="https://github.com/elazar/spl-benchmarks/raw/master/results/spldoublylinkedlist_eps.png" alt="SplDoublyLinkedList - Executions Per Second" width="400" height="225" /></a></td><td><a
title="SplDoublyLinkedList - Memory" href="https://github.com/elazar/spl-benchmarks/raw/master/results/spldoublylinkedlist_memory.png"><img
src="https://github.com/elazar/spl-benchmarks/raw/master/results/spldoublylinkedlist_memory.png" alt="SplDoublyLinkedList - Memory" width="400" height="225" /></a></td><td><strong>Code</strong><a
title="tests/spldoublylinkedlist-array.php at master from elazar&#039;s spl-benchmarks - GitHub" href="https://github.com/elazar/spl-benchmarks/blob/master/tests/spldoublylinkedlist-array.php">Array</a><a
title="tests/spldoublylinkedlist-spl.php at master from elazar&#039;s spl-benchmarks - GitHub" href="https://github.com/elazar/spl-benchmarks/blob/master/tests/spldoublylinkedlist-spl.php">SPL</a></td></tr><tr><td><a
title="SplStack - Executions Per Second" href="https://github.com/elazar/spl-benchmarks/raw/master/results/splstack_eps.png"><img
src="https://github.com/elazar/spl-benchmarks/raw/master/results/splstack_eps.png" alt="SplStack - Executions Per Second" width="400" height="225" /></a></td><td><a
title="SplStack - Memory" href="https://github.com/elazar/spl-benchmarks/raw/master/results/splstack_memory.png"><img
src="https://github.com/elazar/spl-benchmarks/raw/master/results/splstack_memory.png" alt="SplStack - Memory" width="400" height="225" /></a></td><td><strong>Code</strong><a
title="tests/splstack-array.php at master from elazar&#039;s spl-benchmarks - GitHub" href="https://github.com/elazar/spl-benchmarks/blob/master/tests/splstack-array.php">Array</a><a
title="tests/splstack-spl.php at master from elazar&#039;s spl-benchmarks - GitHub" href="https://github.com/elazar/spl-benchmarks/blob/master/tests/splstack-spl.php">SPL</a></td></tr><tr><td><a
title="SplQueue - Executions Per Second" href="https://github.com/elazar/spl-benchmarks/raw/master/results/splqueue_eps.png"><img
src="https://github.com/elazar/spl-benchmarks/raw/master/results/splqueue_eps.png" alt="SplQueue - Executions Per Second" width="400" height="225" /></a></td><td><a
title="SplQueue - Memory" href="https://github.com/elazar/spl-benchmarks/raw/master/results/splqueue_memory.png"><img
src="https://github.com/elazar/spl-benchmarks/raw/master/results/splqueue_memory.png" alt="SplQueue - Memory" width="400" height="225" /></a></td><td><strong>Code</strong><a
title="tests/splqueue-array.php at master from elazar&#039;s spl-benchmarks - GitHub" href="https://github.com/elazar/spl-benchmarks/blob/master/tests/splqueue-array.php">Array</a><a
title="tests/splqueue-spl.php at master from elazar&#039;s spl-benchmarks - GitHub" href="https://github.com/elazar/spl-benchmarks/blob/master/tests/splqueue-spl.php">SPL</a></td></tr><tr><td><a
title="SplMinHeap - Executions Per Second" href="https://github.com/elazar/spl-benchmarks/raw/master/results/splminheap_eps.png"><img
src="https://github.com/elazar/spl-benchmarks/raw/master/results/splminheap_eps.png" alt="SplMinHeap - Executions Per Second" width="400" height="225" /></a></td><td><a
title="SplMinHeap - Memory" href="https://github.com/elazar/spl-benchmarks/raw/master/results/splminheap_memory.png"><img
src="https://github.com/elazar/spl-benchmarks/raw/master/results/splminheap_memory.png" alt="SplMinHeap - Memory" width="400" height="225" /></a></td><td><strong>Code</strong><a
title="tests/splminheap-array.php at master from elazar&#039;s spl-benchmarks - GitHub" href="https://github.com/elazar/spl-benchmarks/blob/master/tests/splminheap-array.php">Array</a><a
title="tests/splminheap-spl.php at master from elazar&#039;s spl-benchmarks - GitHub" href="https://github.com/elazar/spl-benchmarks/blob/master/tests/splminheap-spl.php">SPL</a></td></tr><tr><td><a
title="SplPriorityQueue - Executions Per Second" href="https://github.com/elazar/spl-benchmarks/raw/master/results/splpriorityqueue_eps.png"><img
src="https://github.com/elazar/spl-benchmarks/raw/master/results/splpriorityqueue_eps.png" alt="SplPriorityQueue - Executions Per Second" width="400" height="225" /></a></td><td><a
title="SplPriorityQueue - Memory" href="https://github.com/elazar/spl-benchmarks/raw/master/results/splpriorityqueue_memory.png"><img
src="https://github.com/elazar/spl-benchmarks/raw/master/results/splpriorityqueue_memory.png" alt="SplPriorityQueue - Memory" width="400" height="225" /></a></td><td><strong>Code</strong><a
title="tests/splpriorityqueue-array.php at master from elazar&#039;s spl-benchmarks - GitHub" href="https://github.com/elazar/spl-benchmarks/blob/master/tests/splpriorityqueue-array.php">Array</a><a
title="tests/splpriorityqueue-spl.php at master from elazar&#039;s spl-benchmarks - GitHub" href="https://github.com/elazar/spl-benchmarks/blob/master/tests/splpriorityqueue-spl.php">SPL</a></td></tr><tr><td><a
title="SplObjectStorage - Executions Per Second" href="https://github.com/elazar/spl-benchmarks/raw/master/results/splobjectstorage_eps.png"><img
src="https://github.com/elazar/spl-benchmarks/raw/master/results/splobjectstorage_eps.png" alt="SplObjectStorage - Executions Per Second" width="400" height="225" /></a></td><td><a
title="SplObjectStorage - Memory" href="https://github.com/elazar/spl-benchmarks/raw/master/results/splobjectstorage_memory.png"><img
src="https://github.com/elazar/spl-benchmarks/raw/master/results/splobjectstorage_memory.png" alt="SplObjectStorage - Memory" width="400" height="225" /></a></td><td><strong>Code</strong><a
title="tests/splobjectstorage-array.php at master from elazar&#039;s spl-benchmarks - GitHub" href="https://github.com/elazar/spl-benchmarks/blob/master/tests/splobjectstorage-array.php">Array</a><a
title="tests/splobjectstorage-spl.php at master from elazar&#039;s spl-benchmarks - GitHub" href="https://github.com/elazar/spl-benchmarks/blob/master/tests/splobjectstorage-spl.php">SPL</a></td></tr></tbody></table><h3>Other Data Structures</h3><p>If you have an interest in other data structure implementations for PHP outside of SPL offerings, check out the <a
title="PECL :: Package :: bloomy" href="http://pecl.php.net/package/bloomy">bloomy</a> <a
title="PECL :: The PHP Extension Community Library" href="http://pecl.php.net/">PECL</a> extension, which is an implementation of a <a
title="Bloom filter - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/Bloom_filter">bloom filter</a> created by <a
title="Bloom Filters Quickie - Andrei Zmievski" href="http://zmievski.org/2009/04/bloom-filters-quickie">Andrei Zmievski</a>.</p> ]]></content:encoded> <wfw:commentRss>http://matthewturland.com/2010/05/20/new-spl-features-in-php-5-3/feed/</wfw:commentRss> <slash:comments>9</slash:comments> </item> <item><title>Renaming a DOMNode in PHP</title><link>http://matthewturland.com/2010/02/09/renaming-a-domnode-in-php/</link> <comments>http://matthewturland.com/2010/02/09/renaming-a-domnode-in-php/#comments</comments> <pubDate>Wed, 10 Feb 2010 01:07:14 +0000</pubDate> <dc:creator>Matthew Turland</dc:creator> <category><![CDATA[PHP]]></category> <category><![CDATA[DOM]]></category> <category><![CDATA[HTML]]></category> <category><![CDATA[Web Scraping]]></category> <category><![CDATA[XML]]></category><guid
isPermaLink="false">http://matthewturland.com/?p=218</guid> <description><![CDATA[A recent work assignment had me using PHP to pull HTML data into a DOMDocument instance and renaming some elements, such as b to strong or i to em. As it turns out, renaming elements using the DOM extension is rather tedious. Version 3 of the DOM standard introduces a renameNode() method, but the PHP [...]]]></description> <content:encoded><![CDATA[<p>A recent work assignment had me using PHP to pull HTML data into a <code><a
title="PHP: DOMDocument - Manual" href="http://php.net/manual/en/class.domdocument.php">DOMDocument</a></code> instance and renaming some elements, such as <a
title="HTML element - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/HTML_element#Presentation">b to strong or i to em</a>. As it turns out, renaming elements using the DOM extension is rather tedious.</p><p>Version 3 of the DOM standard introduces a <code><a
title="Document Object Model Core" href="http://www.w3.org/TR/DOM-Level-3-Core/core.html#Document3-renameNode">renameNode()</a></code> method, but the PHP DOM extension doesn&#8217;t currently support it.</p><p>The <code><a
title="PHP: DOMNode - Manual" href="http://php.net/manual/en/class.domnode.php#domnode.props.nodename">$nodeName</a></code> property of the <code><a
title="PHP: DOMNode - Manual" href="http://php.net/manual/en/class.domnode.php">DOMNode</a></code> class is read-only, so it can&#8217;t be changed that way.</p><p>A node can be created with a different name in the same document, but if you specify a value to go along with it, any entities in that value are automatically encoded, so it&#8217;s not possible to pass in the intended inner content of a node if it contains other nodes.</p><p>The only method I&#8217;ve found that works is to replicate the attributes and child nodes of the original node. Attributes are fairly easy, but I ran into an issue replicating children where only the first child of any given node was replicated within its intended replacement and the remaining children were omitted. Here&#8217;s the original code that was exhibiting this behavior.</p><pre class="brush: php; title: ; notranslate">foreach ($oldNode-&gt;childNodes as $childNode) {
    $newNode-&gt;appendChild($childNode);
}</pre><p>The reason for this behavior is that the <code><a
title="PHP: DOMNode - Manual" href="http://php.net/manual/en/class.domnode.php#domnode.props.childnodes">$childNodes</a></code> property of <code>$oldNode</code> is implicitly modified when <code>$childNode</code> is transferred from it to <code>$newNode</code>, so the internal pointer of <code>$childNodes</code> to the next child in the list is no longer accurate.</p><p>To get around this, I took advantage of the fact that any node with any child nodes will always have a <code><a
title="PHP: DOMNode - Manual" href="http://php.net/manual/en/class.domnode.php#domnode.props.firstchild">$firstChild</a></code> property pointing to the first one. The modified code that takes this approach is below and has the behavior I originally set out to implement.</p><pre class="brush: php; title: ; notranslate">while ($oldNode-&gt;firstChild) {
    $newNode-&gt;appendChild($oldNode-&gt;firstChild);
}</pre><p>If you&#8217;re curious, below is the full code segment for renaming a node.</p><pre class="brush: php; title: ; notranslate">$newNode = $oldNode-&gt;ownerDocument-&gt;createElement('new_element_name');
if ($oldNode-&gt;attributes-&gt;length) {
    foreach ($oldNode-&gt;attributes as $attribute) {
        $newNode-&gt;setAttribute($attribute-&gt;nodeName, $attribute-&gt;nodeValue);
    }
}
while ($oldNode-&gt;firstChild) {
    $newNode-&gt;appendChild($oldNode-&gt;firstChild);
}
$oldNode-&gt;ownerDocument-&gt;replaceChild($newNode, $oldNode);</pre><p>Another potential &#8220;gotcha&#8221; is the argument order of the <code><a
title="PHP: DOMNode::replaceChild - Manual" href="http://php.net/manual/en/domnode.replacechild.php">replaceChild()</a></code> method, which is the new node followed by the old node rather than the reverse that most people might expect. Thanks to <a
title="joshua may (notjosh) on Twitter" href="http://twitter.com/notjosh">Joshua May</a> for pointing that one out to me; I might never have understood why I was getting a <a
title="PHP: DOMNode::appendChild - Manual" href="http://php.net/manual/en/domnode.appendchild.php#domnode.appendchild.errors">&#8220;Not Found Error&#8221;</a> <code><a
title="PHP: DOMException - Manual" href="http://php.net/manual/en/class.domexception.php">DOMException</a></code> otherwise.</p> ]]></content:encoded> <wfw:commentRss>http://matthewturland.com/2010/02/09/renaming-a-domnode-in-php/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Splitting PHP Class Files</title><link>http://matthewturland.com/2010/01/22/splitting-php-class-files/</link> <comments>http://matthewturland.com/2010/01/22/splitting-php-class-files/#comments</comments> <pubDate>Fri, 22 Jan 2010 23:26:29 +0000</pubDate> <dc:creator>Matthew Turland</dc:creator> <category><![CDATA[PHP]]></category> <category><![CDATA[Github]]></category> <category><![CDATA[SOAP]]></category> <category><![CDATA[Tokenizer]]></category> <category><![CDATA[WSDL]]></category><guid
isPermaLink="false">http://matthewturland.com/?p=94</guid> <description><![CDATA[A recent work project required me to write a PHP script to interact with a remote SOAP service. Part of the service provider&#8217;s recommended practices entailed using a slightly dated software package called wsdl2php, which generates a single PHP file containing classes corresponding to all user-defined types from a specified WSDL file. The issue I ran into [...]]]></description> <content:encoded><![CDATA[<p>A recent work project required me to write a PHP script to interact with a remote <a
title="SOAP - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/SOAP_(protocol)">SOAP</a> service. Part of the service provider&#8217;s recommended practices entailed using a slightly dated software package called <a
title="wsdl2php | Get wsdl2php at SourceForge.net" href="http://sourceforge.net/projects/wsdl2php/">wsdl2php</a>, which generates a single PHP file containing classes corresponding to all user-defined types from a specified <a
title="Web Services Description Language - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/Web_Services_Description_Language">WSDL</a> file.</p><p>The issue I ran into was due to all the generated PHP classes being housed in a single file. I had to process two WSDL files that had several identical user-defined types in common. As a result, I couldn&#8217;t simply include the two PHP files generated from them because PHP doesn&#8217;t allow you to define two classes with the same name.</p><p>Looking at its source code, modifying wsdl2php to change this behavior was not a very appealing option. Attempting to consolidate the two WSDL files into one with no redundant user-defined type declarations seemed futile as well. Instead, I resolved to split the generated PHP files such that each class was contained in its own file. This would also allow me to use an autoloader to determine which of the classes I actually needed for the particular service call I was making.</p><p>Due to the number of classes, splitting the classes into separate files by hand would have been tedious and time-consuming. I decided to tap into my <a
title="Coding Standard Analysis using PHP_CodeSniffer | Blue Parabola, LLC" href="http://blueparabola.com/blog/coding-standard-analysis-using-phpcodesniffer">previous experience</a> with the <a
title="PHP: Tokenizer - Manual" href="http://us.php.net/tokenizer">tokenizer extension</a> to throw together a CLI script that would handle this for me. Once I got it working, it clocked in at just over 50 <acronym
title="Lines of Code">LOC</acronym> with comments and whitespace. You simply call it from a shell and pass it the PHP file you want to split and the destination for the split class files.</p><p>I thought it might be useful for others needed to process similarly formatted source code, so I threw it into a <a
title="php-class-splitter.php at master from elazar&#039;s php-class-splitter - GitHub" href="https://github.com/elazar/php-class-splitter/blob/master/php-class-splitter.php">github repository</a> for anyone who might like to take a look. I&#8217;m open to suggestions for improvements to implement if enough people find it useful. Feel free to file an issue on the repository if you happen to find a bug.</p> ]]></content:encoded> <wfw:commentRss>http://matthewturland.com/2010/01/22/splitting-php-class-files/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Speaking at a Conference</title><link>http://matthewturland.com/2010/01/20/speaking-at-a-conference/</link> <comments>http://matthewturland.com/2010/01/20/speaking-at-a-conference/#comments</comments> <pubDate>Wed, 20 Jan 2010 16:57:27 +0000</pubDate> <dc:creator>Matthew Turland</dc:creator> <category><![CDATA[PHP]]></category> <category><![CDATA[Conferences]]></category><guid
isPermaLink="false">http://matthewturland.com/?p=83</guid> <description><![CDATA[I can&#8217;t make any claim to the title of veteran conference speaker. Not yet, at least. However, I have done it once before at ZendCon in 2008 and I&#8217;ll be doing it again at php&#124;tek this year. I thought I&#8217;d take a blog post to give out a few tips to any prospective first-time speakers [...]]]></description> <content:encoded><![CDATA[<p>I can&#8217;t make any claim to the title of veteran conference speaker. Not yet, at least. However, I have done it once before at <a
title="ZendCon 2010 ::" href="http://www.zendcon.com/">ZendCon</a> in 2008 and I&#8217;ll be doing it again at <a
title="PHP Conference — Chicago IL May 18-22 2010 — PHP, MySQL, Linux, Windows, Drupal, WordPress" href="http://tek.phparch.com/">php|tek</a> this year. I thought I&#8217;d take a blog post to give out a few tips to any prospective first-time speakers based on my first speaking experience. I&#8217;m assuming there that you&#8217;ve already decided on a particular conference that you <a
title="Why Should I Attend a Conference? | CaseySoftware, LLC - Supporters &amp; Developers of web2project" href="http://caseysoftware.com/blog/why-should-i-attend-a-conference">want to attend</a>, you&#8217;ve <a
title="How to Submit a Conference Talk - LornaJane" href="http://www.lornajane.net/posts/2008/How-to-Submit-a-Conference-Talk">submitted a session proposal</a>, and you&#8217;ve been accepted.</p><p>First, in addition to the <a
title="Conference Travel Tips | CaseySoftware, LLC - Supporters &amp; Developers of web2project" href="http://caseysoftware.com/blog/conference-travel-tips">other things you should do</a> before attending, <strong>be ready to give your presentation before you get on the plane</strong>. You should start on your slides as far in advance as possible. Don&#8217;t put it off or wait until the last minute, because it will likely be more work than you anticipate. This includes making sure that any live demos you intend to give will run as expected. Syntax errors and crashing web servers look very bad to the audience.</p><p>One of the reasons for this is that you&#8217;ll want to <strong>practice your talk out loud</strong>. It&#8217;s one thing to put the material onto slides, but it may sound different when it&#8217;s actually coming out of your mouth and going into the crowd. You may find stumbling points, places where you stutter or get caught off-guard when transitioning from one topic to another. Try to organize the presentation such that it matches your natural flow when talking about the topic without any slides at all.</p><p>Which reminds me, <strong>learn from the masters</strong>. People like Marco Tabini have spoken before and have a <a
title="Some tips for great presentations « The Accidental Businessman" href="http://blog.tabini.ca/2009/11/some-tips-for-great-presentations/">wealth of knowledge</a> that they&#8217;ll share fairly freely most of the time, especially if alcohol (or, in Marco&#8217;s case, an espresso) is involved. Look at books like <a
title="Amazon.com: Presentation Zen: Simple Ideas on Presentation Design and Delivery (9780321525659): Garr Reynolds: Books" href="http://www.amazon.com/Presentation-Zen-Simple-Design-Delivery/dp/0321525655/ref=sr_1_2?ie=UTF8&amp;s=books&amp;qid=1263860360&amp;sr=8-2">Presentation Zen</a> by Garr Reynolds. Take the time to hone your presentation skills before you have to make your delivery.</p><p>If you&#8217;ve been to a conference before, you&#8217;ve probably already learned about my next point the hard way. <strong>Don&#8217;t depend on wifi internet access availability</strong>. Why not? Because the vast majority of the time, <a
title="Wifi at conferences: why it sucks « The Accidental Businessman" href="http://blog.tabini.ca/2009/10/wifi-at-conferences-why-it-sucks/">it will suck</a>. There won&#8217;t be enough IP addresses, someone will do something to hog bandwidth and make latency skyrocket, it will find some way to refuse to work. Save local copies of files, write a minimal daemon to simulate a remote server, do whatever you need to do to avoid it.</p><p>That point goes hand in hand with this one: <strong>test your equipment early and have a Plan B</strong>. In particular, hook your laptop up to the projector in the room in which you&#8217;ll be speaking (or to a test projector, if the conference hosts provide one and prefer you use that) to make sure it can display your slides. <a
title="Ben Ramsey" href="http://benramsey.com/">Ben Ramsey</a> was gracious enough to loan me his Macbook at ZendCon because my Sony Vaio refused to work with the projector and the time-sensitive situation did nothing but add to my speaking nerves. Make sure you don&#8217;t end up in the same spot.</p><p>Lastly, <strong>don&#8217;t let critical reception deter you from speaking again</strong>. I got pretty negative feedback the first time around, but I took it in stride. While I know I have plenty of room for improvement, I&#8217;m still going to give it another shot. Do your very best, then strive to be better.</p><p>Hope you enjoyed this blog post and gleaned something useful from it. If you&#8217;ve got any of your own speaking tips, please feel free to add a comment on this post. If you&#8217;ll be attending php|tek, I look forward to seeing you there!</p> ]]></content:encoded> <wfw:commentRss>http://matthewturland.com/2010/01/20/speaking-at-a-conference/feed/</wfw:commentRss> <slash:comments>10</slash:comments> </item> <item><title>Speaking at tek-X</title><link>http://matthewturland.com/2010/01/06/speaking-at-tek-x/</link> <comments>http://matthewturland.com/2010/01/06/speaking-at-tek-x/#comments</comments> <pubDate>Wed, 06 Jan 2010 21:35:58 +0000</pubDate> <dc:creator>Matthew Turland</dc:creator> <category><![CDATA[PHP]]></category> <category><![CDATA[Conferences]]></category><guid
isPermaLink="false">http://matthewturland.com/?p=70</guid> <description><![CDATA[As the recently released schedule shows, I will be speaking at the php&#124;tek 2010 conference. The session I&#8217;ll be presenting is entitled &#8220;New SPL Features in PHP 5.3&#8243; and it will be an extended version of the webcast I presented as part of the CodeWorks webcast series. While there, I also plan on participating in [...]]]></description> <content:encoded><![CDATA[<p>As the recently released <a
title="Schedule | PHP Conference Chicago May 2010 - PHP, MySQL, Linux, Windows, Drupal, WordPress - TEK·X" href="http://tek.phparch.com/schedule/">schedule</a> shows, I will be speaking at the php|tek 2010 conference. The session I&#8217;ll be presenting is entitled &#8220;New SPL Features in PHP 5.3&#8243; and it will be an extended version of the webcast I presented as part of the <a
title="CodeWorks 09 Free Webcast Series - PHP Magazine, PHP Training, PHP Conferences, PHP Books, PHP Apparel - php|architect" href="http://www.phparch.com/conferences/webcasts" class="broken_link">CodeWorks webcast series</a>.</p><p>While there, I also plan on participating in the <a
title="You can Hack if you wanna.. | PHP Conference Chicago May 2010 - PHP, MySQL, Linux, Windows, Drupal, WordPress - TEK·X" href="http://tek.phparch.com/2009/12/you-can-hack-if-you-wanna/" class="broken_link">Hack Track</a> and may try to recruit a few new contributors (like you!) for the <a
title="Phergie - A PHP 5 IRC Bot" href="http://phergie.org">Phergie</a> project. I am very much looking forward to the event and hope to see you there!</p> ]]></content:encoded> <wfw:commentRss>http://matthewturland.com/2010/01/06/speaking-at-tek-x/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>Database Testing with PHPUnit and MySQL</title><link>http://matthewturland.com/2010/01/04/database-testing-with-phpunit-and-mysql/</link> <comments>http://matthewturland.com/2010/01/04/database-testing-with-phpunit-and-mysql/#comments</comments> <pubDate>Mon, 04 Jan 2010 16:00:47 +0000</pubDate> <dc:creator>Matthew Turland</dc:creator> <category><![CDATA[PHP]]></category> <category><![CDATA[MySQL]]></category> <category><![CDATA[PHPUnit]]></category> <category><![CDATA[Testing]]></category><guid
isPermaLink="false">http://matthewturland.com/?p=49</guid> <description><![CDATA[Update 2012/01/15: I finally got around to submitting a patch to document this feature in the PHPUnit manual. Sebastian has merged it, so it will hopefully be available in the online manual soon. Update #2 2012/01/23: I got around to checking the online version of the manual and the current build includes my patch. Enjoy. [...]]]></description> <content:encoded><![CDATA[<p><strong>Update</strong> 2012/01/15: I finally got around to submitting a patch to document this feature in the PHPUnit manual. Sebastian has <a
title="#45: Added a section on MySQL XML datasets to the Database Testing chapter by elazar for sebastianbergmann/phpunit-documentation - Pull Request - GitHub" href="https://github.com/sebastianbergmann/phpunit-documentation/pull/45">merged it</a>, so it will hopefully be available in the online manual soon.</p><p><strong>Update #2</strong> 2012/01/23: I got around to checking the online version of the manual and the current build <a
title="Chapter 8. Database Testing" href="http://www.phpunit.de/manual/current/en/database.html#mysql-xml-dataset">includes my patch</a>. Enjoy.</p><p>I recently made a contribution to the <a
title="PHPUnit" href="https://github.com/sebastianbergmann/phpunit/">PHPUnit</a> project that I thought I&#8217;d take a blog post to discuss. One of the extensions bundled with PHPUnit adds support for <a
title="Chapter 9. Database Testing" href="http://www.phpunit.de/manual/3.4/en/database.html">database testing</a>. This extension was contributed by <a
title="Digital Sandwich" href="http://www.ds-o.com/">Mike Lively</a> and is a port of the <a
title="DbUnit - Core Components" href="http://www.dbunit.org/components.html">DbUnit</a> extension for the <a
title="Welcome to JUnit.org! | JUnit.org" href="http://www.junit.org/">JUnit</a> Java unit testing framework. If you&#8217;re interested in learning more about database unit testing, check out <a
title="Testing PHP/MySQL Applications with PHPUnit/DbUnit  - Sebastian Bergmann" href="http://sebastian-bergmann.de/archives/773-Testing-PHPMySQL-Applications-with-PHPUnitDbUnit.html">this presentation</a> by <a
title="Sebastian Bergmann  - Sebastian Bergmann" href="http://sebastian-bergmann.de/">Sebastian Bergmann</a> on the subject.</p><p>One of the major components of both extensions is the data set. Database unit tests involve loading a seed data set into a database, executing code that performs an operation on that data set such as deleting a record, and then checking the state of the data set to confirm that the operation had the desired effect. DbUnit supports <a
title="DbUnit - Core Components" href="http://www.dbunit.org/components.html">multiple formats</a> for seed data sets. The PHPUnit Database extension includes <a
title="Chapter 9. Database Testing" href="http://www.phpunit.de/manual/3.4/en/database.html#database.datasets">support</a> for DbUnit&#8217;s XML and flat XML formats plus <acronym
title="Comma Separated Values">CSV</acronym> format as well.</p><p>If you&#8217;re using <a
title="MySQL ::  The world's most popular open source database" href="http://www.mysql.com/">MySQL</a> as your database, CSV has been the only format supported by both the <a
title="MySQL ::   MySQL 5.0 Reference Manual :: 4.5.4 mysqldump — A Database Backup Program" href="http://dev.mysql.com/doc/refman/5.0/en/mysqldump.html#option_mysqldump_fields">mysqldump</a> utility and the PHPUnit Database extension up to this point. My contribution adds support for its <a
title="MySQL ::   MySQL 5.0 Reference Manual :: 4.5.4 mysqldump — A Database Backup Program" href="http://dev.mysql.com/doc/refman/5.0/en/mysqldump.html#option_mysqldump_xml">XML format</a> to the extension. While this support was developed to work in the PHPUnit 3.4.x branch, it won&#8217;t be available in a stable release until 3.5.0. In the meantime, this is how you can use it now.</p><ol><li>Go to the <a
title="Commit fad913fd84720f889e1d3415e775f68304e76f52 to elazar&#039;s phpunit - GitHub" href="https://github.com/sebastianbergmann/phpunit/commit/fad913fd84720f889e1d3415e775f68304e76f52">commit</a> on Github and apply the additions and modifications included in it to your PHPUnit installation.</li><li>From a shell, get your XML seed data set and store it in a location accessible to your unit test cases.<pre class="brush: bash; title: ; notranslate">mysqldump --xml -t -u username -p database &gt; seed.xml</pre></li><li>Create a test case class that extends PHPUnit_Extensions_Database_TestCase. Implement getConnection() and getDataSet() as per the documentation where the latter will include a method call to create the data set from the XML file as shown below.<pre class="brush: php; title: ; notranslate">$dataSet = $this-&gt;createMySQLXMLDataSet('/path/to/seed.xml');</pre></li><li>At this point, you can execute operations on the database to get it to its expected state following a test, produce an XML dump of the database in that state, and then compare that dump to the actual database contents in a test method to confirm that the two are equal.<pre class="brush: php; title: ; notranslate">$expected = $this-&gt;createMySQLXMLDataSet('/path/to/expected.xml');
$actual = new PHPUnit_Extension_Database_DataSet_QueryDataSet($this-&gt;getConnection());
// Specify a SELECT query as the 2nd parameter here to limit the data set, else the entire table is used
$actual-&gt;addTable('tablename');
$this-&gt;assertDataSetsEqual($expected, $actual);</pre></li></ol><p>That&#8217;s it! Hopefully this proves useful to <a
title="Twitter / Trevor Morse: @elazar OMG, yes! I've bee ..." href="http://twitter.com/trevor_morse/status/7239323093">someone else</a>.</p> ]]></content:encoded> <wfw:commentRss>http://matthewturland.com/2010/01/04/database-testing-with-phpunit-and-mysql/feed/</wfw:commentRss> <slash:comments>12</slash:comments> </item> <item><title>PHPUnit and Xdebug on Ubuntu Karmic</title><link>http://matthewturland.com/2010/01/03/phpunit-and-xdebug-on-ubuntu-karmic/</link> <comments>http://matthewturland.com/2010/01/03/phpunit-and-xdebug-on-ubuntu-karmic/#comments</comments> <pubDate>Sun, 03 Jan 2010 16:00:20 +0000</pubDate> <dc:creator>Matthew Turland</dc:creator> <category><![CDATA[PHP]]></category> <category><![CDATA[PHPUnit]]></category> <category><![CDATA[Ubuntu]]></category> <category><![CDATA[Xdebug]]></category><guid
isPermaLink="false">http://matthewturland.com/?p=62</guid> <description><![CDATA[This is just a quick post to advise anyone who may be using PHPUnit and Xdebug together on Ubuntu Karmic. If you try to upgrade to PHPUnit 3.4.6 and you&#8217;re using the php5-xdebug Ubuntu package (which is Xdebug 2.0.4), you may get output that looks like this: There are two ways to deal with this [...]]]></description> <content:encoded><![CDATA[<p>This is just a quick post to advise anyone who may be using <a
title="PHPUnit" href="https://github.com/sebastianbergmann/phpunit/">PHPUnit</a> and <a
title="Xdebug - Debugger and Profiler Tool for PHP" href="http://xdebug.org/">Xdebug</a> together on <a
title="Ubuntu Home Page | Ubuntu" href="http://www.ubuntu.com/">Ubuntu Karmic</a>. If you try to upgrade to PHPUnit 3.4.6 and you&#8217;re using the <a
title="Ubuntu -- Details of package php5-xdebug in karmic" href="http://packages.ubuntu.com/karmic/php5-xdebug">php5-xdebug</a> Ubuntu package (which is Xdebug 2.0.4), you may get output that looks like this:</p><pre class="brush: plain; title: ; notranslate">$ sudo pear upgrade phpunit/PHPUnit
Did not download optional dependencies: pear/Image_GraphViz, pear/Log, use --alldeps to download automatically
phpunit/PHPUnit can optionally use package &quot;pear/Image_GraphViz&quot; (version &gt;= 1.2.1)
phpunit/PHPUnit can optionally use package &quot;pear/Log&quot;
phpunit/PHPUnit can optionally use PHP extension &quot;pdo_sqlite&quot;
phpunit/PHPUnit requires PHP extension &quot;xdebug&quot; (version &gt;= 2.0.5), installed version is 2.0.4
No valid packages found
upgrade failed</pre><p>There are two ways to deal with this situation. First off, note that the newer Xdebug 2.0.5 version includes <a
title="Xdebug: Updates" href="http://xdebug.org/updates.php">several bugfixes</a> including one related to code coverage reporting. That said, if you still want to continue using the php5-xdebug package anyway, you can force the upgrade by having the PEAR installer ignore dependencies like so:</p><pre class="brush: bash; title: ; notranslate">sudo pear upgrade -n phpunit/PHPUnit</pre><p>The other method involves installing Xdebug 2.0.5. First, if you have the php5-xdebug package, remove it.</p><pre class="brush: bash; title: ; notranslate">sudo apt-get remove php5-xdebug</pre><p>Next, use the PECL installer to install Xdebug. This requires that you have the php5-dev package installed so that the extension can be compiled locally.</p><pre class="brush: bash; title: ; notranslate">sudo apt-get install php5-dev
sudo pecl install xdebug</pre><p>At this point, create the file /etc/php5/conf.d/xdebug.ini if it doesn&#8217;t already exist and populate it with these contents:</p><pre class="brush: plain; title: ; notranslate">zend_extension=/usr/lib/php5/20060613/xdebug.so</pre><p>Then bounce Apache so that the new extension will be loaded.</p><pre class="brush: bash; title: ; notranslate">sudo apache2ctl restart</pre><p>That&#8217;s it. Hope someone finds this helpful.</p> ]]></content:encoded> <wfw:commentRss>http://matthewturland.com/2010/01/03/phpunit-and-xdebug-on-ubuntu-karmic/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> <item><title>I&#8217;m a Honey Pot</title><link>http://matthewturland.com/2010/01/01/im-a-honey-pot/</link> <comments>http://matthewturland.com/2010/01/01/im-a-honey-pot/#comments</comments> <pubDate>Sat, 02 Jan 2010 04:01:03 +0000</pubDate> <dc:creator>Matthew Turland</dc:creator> <category><![CDATA[PHP]]></category> <category><![CDATA[WordPress]]></category><guid
isPermaLink="false">http://matthewturland.com/?p=39</guid> <description><![CDATA[Side note: Yes, the title of this post is a throwback to the 418 status code in the HTTP protocol. My sense of humor is just odd that way. I thought I&#8217;d kick things off on my new blog with a quick post on something I did while getting it set up. Before switching to [...]]]></description> <content:encoded><![CDATA[<p><em>Side note: Yes, the title of this post is a throwback to the <a
title="List of HTTP status codes - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_Error">418 status code</a> in the HTTP protocol. My sense of humor is just odd that way.</em></p><p>I thought I&#8217;d kick things off on my new blog with a quick post on something I did while getting it set up.</p><p>Before switching to this new blog, I&#8217;d moved to using the <a
title="habari-extras - Revision 2625: /plugins/spamhoneypot/trunk" href="http://svn.habariproject.org/habari-extras/plugins/spamhoneypot/trunk/">spamhoneypot</a> plugin on my old <a
title="Habari Project" href="http://habariproject.org/en/">Habari</a> blog to capture spam. I had a great amount of success in that switch, but in deciding to move to using <a
title="WordPress › Blog Tool and Publishing Platform" href="http://wordpress.org/">WordPress</a> on this new blog, I noticed that it had no equivalent <a
title="WordPress › WordPress Plugins" href="http://wordpress.org/extend/plugins/">plugins</a>. There were several anti-spam plugins, but they all required use of a third-party service. I hadn&#8217;t seen consistent success with plugins that used this approach in the past, so I wanted to avoid repeating those experiences.</p><p>So, I decided to try my hand at <a
title="Writing a Plugin « WordPress Codex" href="http://codex.wordpress.org/Writing_a_Plugin">writing a WordPress plugin</a>. After wading through the <a
title="Plugin API/Filter Reference « WordPress Codex" href="http://codex.wordpress.org/Plugin_API/Filter_Reference#Database_Writes_2">filter</a> and <a
title="Plugin API/Action eference « WordPress Codex" href="http://codex.wordpress.org/Plugin_API/Action_Reference#Comment.2C_Ping.2C_and_Trackback_Actions">action</a> documentation and googling around for a bit, I came up with a fairly simple plugin that seems to do the job.</p><p>The plugin works by adding a textarea field to the comment form that&#8217;s hidden using a CSS style. Since bots don&#8217;t generally detect CSS like this, they proceed to fill out the field like any other field. This implies that they aren&#8217;t a human being using a browser, in which case the plugin marks the comment as spam. I&#8217;ve found this catches the vast majority of spam comments with very few false results.</p><p>I&#8217;ve submitted to have the plugin hosted on the WordPress site, but until then, you can grab a copy off of a <a
title="elazar&#039;s wp-spam-honeypot at master - GitHub" href="https://github.com/elazar/wp-spam-honeypot">Github repository</a> I&#8217;ve set up for it. Hope you find it useful!</p><p><strong>Update 1/2/10 8:41 AM CST</strong>: The plugin is now available for download from the <a
title="WordPress › Spam Honey Pot « WordPress Plugins" href="http://wordpress.org/extend/plugins/spam-honeypot/">WordPress site</a>.</p> ]]></content:encoded> <wfw:commentRss>http://matthewturland.com/2010/01/01/im-a-honey-pot/feed/</wfw:commentRss> <slash:comments>10</slash:comments> </item> <item><title>The Configuration Pattern in Zend Framework</title><link>http://matthewturland.com/2009/09/25/the-configuration-pattern-in-zend-framework/</link> <comments>http://matthewturland.com/2009/09/25/the-configuration-pattern-in-zend-framework/#comments</comments> <pubDate>Fri, 25 Sep 2009 23:00:21 +0000</pubDate> <dc:creator>Matthew Turland</dc:creator> <category><![CDATA[Design Patterns]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Zend Framework]]></category><guid
isPermaLink="false"></guid> <description><![CDATA[Several components in Zend Framework such as Zend_Form and Zend_Layout, support what I&#8217;ve come to call the Configuration Pattern. Though it doesn&#8217;t appear to exist in any officially sanctioned capacity like traditional design patterns, and it may qualify more as a convention than a design pattern, it seems to me that it&#8217;s something worth knowing [...]]]></description> <content:encoded><![CDATA[<p>Several components in <a
href="http://framework.zend.com/" title="Zend Framework">Zend Framework</a> such as <a
href="http://framework.zend.com/manual/en/zend.form.html" title="Zend Framework: Documentation">Zend_Form</a> and <a
href="http://framework.zend.com/manual/en/zend.layout.html" title="Zend Framework: Documentation">Zend_Layout</a>, support what I&#8217;ve come to call the Configuration Pattern. Though it doesn&#8217;t appear to exist in any officially sanctioned capacity like <a
href="http://en.wikipedia.org/wiki/Design_pattern_(computer_science)" title="Design pattern (computer science) - Wikipedia, the free encyclopedia">traditional design patterns</a>, and it may qualify more as a convention than a design pattern, it seems to me that it&#8217;s something worth knowing about.</p><p>Here&#8217;s how it works. Have a look at the constructor for Zend_Form. It accepts an $options parameter, which can be an associative array or Zend_Config instance. If it&#8217;s an array, setOptions() is called. If it&#8217;s a Zend_Config instance, setConfig() is called, which then converts the Zend_Config instance to an associative array and passes that to setOptions(). So, either way, you end up in the same method with the same type of data.</p><p>setOptions() then iterates over the associative array it receives. It takes the index of each element and looks for a corresponding <a
href="http://en.wikipedia.org/wiki/Mutator_method" title="Mutator method - Wikipedia, the free encyclopedia">setter method</a>. For example, if the array contains an element with the index &#8216;viewScriptPath&#8217;, setOptions() would check the class definition for a method setViewScriptPath() using method_exists(). Since that method does exist in Zend_Form, it would be called and passed the value from the associative array corresponding to that index.</p><p>This alleviates the need to explicitly call a setter method for every value you want to set. While that approach is slightly more efficient &mdash; not using setOptions() means one less function call and no method_exists() calls, which are slightly expensive &mdash; it can make code using the class in question look overly verbose or cluttered.</p><p>Alternatively, the setOptions() method could be called after the class was instantiated. With respect to that approach, passing an array or Zend_Config instance to the constructor only saves you one line of code on the calling end.</p><p>To get a list of classes that use this pattern, you can issue the bash command shown below from the library directory of your Zend Framework installation.</p><pre>grep "\$this-&gt;setOptions" * | sort | uniq</pre>]]></content:encoded> <wfw:commentRss>http://matthewturland.com/2009/09/25/the-configuration-pattern-in-zend-framework/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Breadth-First Thinking</title><link>http://matthewturland.com/2009/08/03/breadth-first-thinking/</link> <comments>http://matthewturland.com/2009/08/03/breadth-first-thinking/#comments</comments> <pubDate>Mon, 03 Aug 2009 03:28:02 +0000</pubDate> <dc:creator>Matthew Turland</dc:creator> <category><![CDATA[Personal]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Research]]></category><guid
isPermaLink="false"></guid> <description><![CDATA[A surprisingly frequent occurrence in my day-to-day life goes something like this: I&#8217;ll get into IM or IRC conversations with friends when one technical topic or another will come up. Sometimes the conversation just branches from one tangent to another until that happens, other times the friend will ping me to ask a particular question [...]]]></description> <content:encoded><![CDATA[<p>A surprisingly frequent occurrence in my day-to-day life goes something like this: I&#8217;ll get into IM or IRC conversations with friends when one technical topic or another will come up. Sometimes the conversation just branches from one tangent to another until that happens, other times the friend will ping me to ask a particular question on the topic. <a
title="#followfriday – Those That Influence Me Most | BrandonSavage.net" href="http://www.brandonsavage.net/five-influential-php-developers-followfriday/">Some friends</a> have even come to know this as a notable quality of mine.</p><p>The phrase that I&#8217;ve used to describe this quality in my head is &#8220;breadth-first thinking.&#8221; I thought I&#8217;d take a blog post to describe it in a bit more depth. You can find some of this information in the <a
title="Chris Shiflett: PHP Advent Calendar Day 11" href="http://shiflett.org/blog/2007/dec/php-advent-calendar-day-11">2007 PHP Advent Calendar entry</a> that <a
title="Ben Ramsey" href="http://benramsey.com/">Ben Ramsey</a> did, but I&#8217;ll reiterate some of it here to bring it into context with my personal methods.</p><h3>Social Bookmarking</h3><p>Get an account on a <a
title="List of social software - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/List_of_social_bookmarking_websites#Social_bookmarking">social bookmarking service</a>. I personally like <a
title="Delicious" href="http://www.delicious.com/">Delicious</a> as its <a
title="Quick Tour of Firefox Add-On on Delicious" href="http://www.delicious.com/help/quicktour/firefox">Firefox addon</a> makes bookmarking and <a
title="Tag (metadata) - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/Tag_(metadata)">tagging</a> (which is extremely important for making things easy to find) a Ctl+D and Alt+S away in <a
title="Firefox web browser | Faster, more secure, &amp; customizable" href="http://www.mozilla.org/en-US/firefox/new/?from=getfirefox">Firefox</a>. You&#8217;re only as likely to use this service as it is easy to use and this is going to comprise a significant part of your personal database.</p><h3>Feed Reader</h3><p>Find a <a
title="Aggregator - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/Aggregator">feed reader</a> you like. I use <a
title="Google Reader" href="https://accounts.google.com/ServiceLogin?service=reader&amp;passive=1209600&amp;continue=https://www.google.com/reader&amp;followup=https://www.google.com/reader">Google Reader</a> myself as it&#8217;s relatively frills-free and allows me to use all the functionality I need from the keyboard. Given only a few minutes, it&#8217;s easy to make a pass and mark off items that don&#8217;t interest me.</p><h3>Everything Bucket</h3><p>While Alex Payne may be against them, I think <a
title="Alex Payne — The Case Against Everything Buckets" href="http://al3x.net/2009/01/31/against-everything-buckets.html">everything buckets</a> are still potentially useful tools. Originally I was using Google Notebook, but when that got shut down I had to shop around for an alternative. I had issues with <a
title="Remember Everything. | Evernote Corporation" href="http://www.evernote.com/">Evernote</a> consistently retaining formatting in information I saved to it. I tried a few others and finally settled on using private posts on <a
title="Tumblr" href="https://www.tumblr.com/">Tumblr</a>.</p><h3>News Sites</h3><p>Subscribe to relevant new sites for topics that interest you, but in particular aim for sites that host a variety of information. I find <a
title="PHPDeveloper: PHP News, Views and Community" href="http://phpdeveloper.org/">PHP Developer</a>, <a
title="Planet PHP" href="http://www.planet-php.net/">Planet PHP</a>, and <a
href="http://devzone.zend.com/1235/view-helpers-in-zend-framework/">Zend Developer Zone</a> to be excellent on both counts because they often put the spotlight on experiences using PHP and software based on it in conjunction with other technologies. Don&#8217;t let it stop there, though. Further explore blogs that they syndicate and subscribe to the ones that carry a lot of subject matter you like.</p><h3>Social Media</h3><p>Finally, participate in social media. If you follow people who share your interests on IRC, <a
title="Facebook" href="https://www.facebook.com/">Facebook</a>, or <a
title="Twitter" href="http://twitter.com/">Twitter</a>, links to interesting content are unlikely to be in short supply. If you use a Twitter client like <a
title=&#8221;Spaz: A Twitter, Identi.ca and Laconica client for Palm&Acirc;&reg; Pre&acirc;</p> ]]></content:encoded> <wfw:commentRss>http://matthewturland.com/2009/08/03/breadth-first-thinking/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using apc
Page Caching using disk: enhanced
Database Caching 27/35 queries in 0.010 seconds using apc

Served from: matthewturland.com @ 2012-05-21 17:31:32 -->
