Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Edd Mann:
Tuples in PHP
Apr 18, 2014 @ 14:48:38

Edd Mann has a new post today sharing some of his exploration into implementing tuples in PHP. A tuple is a common data structure in other languages consisting of an immutable, ordered list of items.

Since exploring languages such as Scala and Python which provide the tuple data-structure, I have been keen to experiment with how to clearly map it into a PHP solution. Tuples are simply a finite, ordered sequence of elements - usually with good language support to both pack (construction) and unpack (deconstruction) of the values. I have found that many use-cases of the common place array structure in PHP could be better suited to n-tuple's. [...] I discussed briefly that what makes tuples so powerful in the highlighted languages is their good support for handling their contents, for example unpacking a user tuple into separate id and name variables. PHP supports this form of unpacking in regard to arrays using the 'list' function, which I frequently use to return multiple values from a function/method invocation.

He shares the code for his basic implementation, extended from the SplFixedArray, and shows an example of it in use. He also includes samples showing how to make typed tuples via a "type" method call.

tagged: tuple data structure splfixedarray example tutorial

Link: http://eddmann.com/posts/tuples-in-php/

Davey Shafik's Blog:
Faster Arrays
Nov 07, 2011 @ 14:54:58

In this new post to his blog Davey Shafik looks at an alternative to the traditional arrays most scripts use - something a little faster and more specific: SplFixedArray, part of the Standard PHP Library included with every release.

The SplFixedArray class provides a super-fast, fixed size array implementation. There are some limitations however, first you must use numeric keys and secondly you cannot use anonymous assignment (i.e. $array[] = 'value';). You’ll notice one requirement was missing, that it should have a fixed size. While having a fixed size is what will bring you the speed increase it’s actually not a requirement that the size be fixed.

Because of these restrictions, the SplFixedArray is faster than its cousin - between 20 and 40 percent faster, depending on the size of the array. He includes a few snippets in the the post - one showing how he benchmarked the differences against simple arrays and another showing a more advanced example with another SPL type, a FilterIterator.

tagged: spl splfixedarray array benchmark filteriterator

Link:

Matt Farina's Blog:
SplFixedArray, An Underutilized PHP Gem
Sep 09, 2011 @ 15:43:11

Matt Farina has a new post today looking at an "underutilized gem" he's found in the offerings of the Standard PHP Library (SPL) - the SplFixedArray.

Arrays in PHP are not arrays per the typical array data type. Instead, as Matt Butcher recently pointed out arrays in PHP are similar to hashes in other languages. This can be a very important point to know when tracking down bugs in code and to programmers coming to PHP from other languages. But, what if we wanted something like a traditional array data type? Maybe something that preserved numeric order. Enter SplFixedArray.

He gives an example of using the SplFixedArray object versus the normal array variables in a simple PHP snippet showing the preservation of numbering order. He also touches on the memory consumption difference between the two, with the fixed array coming in quite a bit lower than the normal array data type (around 25% based on his basic testing). There are some catches to using it, though including incompatibility with array methods and the fact that it doesn't implement things like Iterator or Countable interfaces.

tagged: splfixedarray array replacement issues performance memory usage

Link:

Shay Ben Moshe's Blog:
PHP's native array vs SplFixedArray performance
Apr 28, 2011 @ 14:06:01

Shay Ben Moshe has put together a quick post today where he benchmarks array handling performance differences between PHP's native array and the newer SplFixedArray data structure that's a part of the Standard PHP Library that comes with any recent version of the language.

In PHP, arrays are one of the most fundamental data structures. We use them everywhere. They are very flexible, because they are implemented as associative arrays, and therefore let us use both string and integer keys. They are also unlimited in size, in most languages arrays are fixed-sized, but this is not the case in PHP. With that in mind, there still is a drawback. It damages performance. The solution for this problem may be SplFixedArray. But, it is not a perfect solution.

He points out two major differences - the SplFixedArray is, well, a fixed size and the fact that it can only use integer keys (no associative arrays here). He created three tests to compare the performance of the two:

  • Writing data to the array
  • Reading data from the array
  • Getting a random value from the array

Each of these are measured in terms of runtime and/or memory usage. If you'd like to try out the tests for yourself, you can download the files needed. I won't cover the results of the tests here, though - you'll need to visit the post for that!

tagged: native array splfixedarray performance benchmark runtime memory

Link:

Rafael Dohms' Blog:
SPL: a hidden gem
Jun 10, 2009 @ 16:19:06

Earlier this month Rafael Dohms posted a new article to his blog looking at a feature of PHP it seems not every developer knows about - the Standard PHP Library (or SPL).

By a show of hands, how many people here ever heard of SPL? How many already used it? Chances are most of you didn’t raise your hands, and some might even have a confused look on their faces. Indeed that is the sad reality when it comes to SPL, but What is SPL?

He goes on to look at a few different things the SPL has to offer like autoloader overloading, iterators (with an included list of 21 of them) and the SplFixedArray that can be used to help speed up array access and manipulation.

tagged: splfixedarray autoloader iterator spl

Link:


Trending Topics: