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

David Lundgren:
SPL FileObject & LimitIterator
Jun 24, 2015 @ 13:04:24

In the latest post to his site David Lundgren takes a look at two pieces of PHP's SPL libraries - the FileObject and LimitIterator.

Over that last couple of weeks I've come to use the SPL far more than I have in the past. The SplFileObject for reading CSV files is far more convenient than the fgetcsv() function, and associated code needed for a CSV file. Using the LimitIterator allowed me to easily bypass the first row of the CSV, as they were headers and I knew the format of those headers.

He includes an example of using these two to read from a CSV file, processing the header information and each row following. He also gives another example of the LimitIterator handing the results of a database query, reducing the array set down to only the first twelve items. You can find out more about these two handy tools in their SPL documentation, FileObject and LimitIterator, as well as the rest of the SPL if you haven't looked into it before.

tagged: spl standardphplibrary fileobject limititerator csv database results

Link: http://davidscode.com/blog/2015/06/22/spl-fileobject-limititerator/

Three Devs & A Maybe Podcast:
Introduction to the Standard PHP Library (SPL)
Aug 07, 2014 @ 14:44:20

The Three Devs & A Maybe podcast has posted their latest episode (#37) with hosts Michael Budd, Fraser Hart, Lewis Cains and Edd Mann with an introduction to the SPL (Standard PHP Library).

In this weeks show we are discussing all things SPL (Standard PHP Library), used to solve commonly found problems in an OO-PHP manner. After a brief tangent on what each cast member uses for placeholder text/images, we touch upon the key reasons for SPL's development. Following this we highlight a few of the many data-structures (fixed-array, doubly linked-list) and interfaces (countable) available to you. Finally, we wrap up the show with the regular quiz, which you've probably guessed it, is topically about the Standard PHP Library.

Some of the topics mentioned in this latest episode also include:

You can listen to this latest episode either through the in-page player or by downloading the mp3. If you enjoy the episode, consider subscribing to their feed too.

tagged: threedevsandamaybe podcast ep37 introduction spl standardphplibrary

Link: http://threedevsandamaybe.com/posts/introduction-to-the-standard-php-library-spl/

Joshua Thijssen:
SPL Deepdive: RegexIterator
Feb 13, 2014 @ 17:18:55

Joshua Thijssen has posted a deep dive with the RegexIterator to his site today. This iterator is part of the SPL (Standard PHP Library) that is included with PHP. It allows you to filter items based a given regular expression.

If everything goes according to plan (which never is the case), I’ll try and highlight some of the fascinating stuff that can be found inside the SPL. I do a lot of presentations about the SPL, and one of the things I like to tell people is that even though the SPL, – iterators particularly – is a magnificent piece of code that is often underused and misunderstood, it does come with some quirks and glitches that aren’t documented properly. Today, i’ll explain a bit indepth the RegexIterator. This iterator extends the FilterIterator, meaning it can be used to filter out unwanted entries from parent iterators.

He gives a brief example of it in use to filter the results from a DirectoryIterator but quickly gets into the more advanced functionality. He looks at the different "modes" it can operate in: all matches, get matches, match, replace and split. He includes code examples and explanations for each of them, including the resulting output. He also shows how to filter based on the keys instead of values and briefly mentions the "preg_flags" but doesn't get too far into them as they change based on the mode being used.

tagged: spl standardphplibrary regexiterator deepdive tutorial

Link: https://www.adayinthelifeof.nl/2014/02/12/spl-deepdive-regexiterator

Engine Yard Blog:
The Outer Iterator
Jan 13, 2014 @ 17:45:08

On the Engine Yard blog they have a new tutorial posted by Cal Evans looking at an object offered by the Standard PHP Library - the OuterIterator.

When PHP 5 arrived there was great excitement and rejoicing over the new object model. For many of us who struggled through PHP 4′s “Object Based” paradigm, PHP 5 was a ray of sunshine. We were so excited that many of us lost sight of another important addition to PHP 5, the Standard PHP Library (SPL).

Since then, the excitement over the object model has died down. Many authors and speakers have talked about, blogged about, and written about the SPL. However the SPL itself is a very large topic. We are going to narrow our focus down a bit to a subtopic of a specific section of the SPL. This blog post will deal with the OuterIterator. This is an interface defined in the SPL and used by several of the built in iterators.

He goes on to give a brief introduction about what the OuterIterator is and some examples uses for it, including one of the easier to follow - using it with a FilterIterator-based class. He gets into more detail about how the OuterIterator works and includes example snippets to help clarify. His "DwarfPrinter" class extends the FilterIterator and echoes out the names of The Seven Dwarves. He also includes an example of the output showing a more formatted output that follows the nesting and execution of the iterator. He finishes off the post with a list of iterators are useful to pair with the OuterIteator.

tagged: outeriterator tutorial filteriterator standardphplibrary spl

Link: https://blog.engineyard.com/2014/the-outer-iterator

DPCRadio:
Episode #2012-27 - Elizabeth Smith's "SPL in the Wild"
Nov 12, 2012 @ 18:53:38

The Dutch PHP Conference has posted their latest podcast in their DPCRadio series as recorded at this year's Dutch PHP Conference - Elizabeth Smith's SPL in the Wild:

The standard PHP library (SPL) is growing in both maturity and use. But a lot of developers still aren't aware of the tools in SPL or simply haven't seen good examples of how to use the code. From interfaces to an autoload stack to classes that make objects act like arrays, there are tools to make every application leaner and faster, or simply more clever. Using live projects from github, take a look at the good, bad, and the ugly of SPL usage in PHP development.

You can listen to this latest episode either via the in-page player or by downloading it directly. You can also subscribe to their feed and get the latest as they're released.

tagged: dpcradio podcast dpc12 elizabethsmith spl standardphplibrary

Link:

Jeremy Cook's Blog:
Implementing the ArrayAccess Interface
Feb 02, 2012 @ 19:56:43

Jeremy Cook is back with the next part of his series looking at the handy features PHP's SPL provides. In this new post he looks at the ArrayAccess interface and how it can make your data more accessible to PHP's own array handing functions.

ArrayAccess allows you to treat an object that implements it as if it is an array for the purposes of setting, unsetting and retrieving data from it. Please note the emphasis in the last sentence! ArrayAccess does not make an object behave like an array in any other way. If you pass an object that implements ArrayAccess to a PHP array function such as in_array() you'll still get an error. This will become a little clearer with some of the examples below.

He shows what you'll need to use this interface in your class - implementing the interface and defining a set of four methods to get/set and check for the value in your array. He includes a practical example of pulling data back from an API and wrapping it in a class to make accessing it simpler (also implementing the Countable interface as well, see the previous post for more on that). Code is include to illustrate how it can be used.

tagged: arrayaccess spl tutorial array standardphplibrary countable

Link:

Jeremy Cook's Blog:
Using the Countable Interface
Jan 05, 2012 @ 20:39:05

In a recent post to his blog Jeremy Cook has a tutorial about using the Countable interface (part of the SPL) in your objects to make them play nicely with functions like count.

PHP provides a number of predefined interfaces and classes that can really make your life as a developer easier but which are often overlooked. The functionality offered by the Standard PHP Library (SPL) and the predefined interfaces is extremely cool and very powerful but very underutilized. [...] I thought I’d write a few articles with examples of how I’ve used these classes and interfaces in the hope that someone would find it useful. I’d love it if people felt like commenting with their own examples too. I’ll start with a quick look at the Countable interface.

He includes sample code for classes using the Countable interface and defining the custom "count()" method inside. This method lets you define how the object will behave when something like count is called on it. His examples show returning the number of items in a private variable, determining the state of an object and including logic to only find valid data (like from a database table).

tagged: spl standardphplibrary countable interface tutorial

Link:

Jani Hartikainen's Blog:
How to use built-in SPL exception classes for better error handling
May 09, 2011 @ 13:47:12

On his blog today Jani Hartikainen looks at how you can use the SPL exception types to allow for better overall error handling in your application. Things like BadMethodCallException and OutOfBoundsException make the errors much more descriptive.

Since PHP 5, there has been a bundle of built-in exceptions – the "SPL exceptions" – in PHP. However, the documentation for these classes is quite lacking in examples, and it can be difficult to understand when you should be using them. The short answer is always.

The list of exception types he recommends include:

  • BadMethodCallException
  • DomainException
  • LengthException
  • OutOfRangeException
  • UnexpectedValueException

For each he gives an example usage of it, sometimes including a bit of code to illustrate.

tagged: spl standardphplibrary exception handling classes types

Link:

PHPBuilder.com:
Using SPL Iterators in PHP to Interact with Different Data Types
Mar 17, 2011 @ 15:34:09

On PHPBuilder.com today there's a new tutorial from Jason Gilmore about using the SPL with different data types to more effectively work with them more effectively.

One great way to incorporate more OOP into your applications is through the Standard PHP Library (SPL), a powerful yet largely unknown extension made part of the official PHP language with the PHP 5.0 release. [...] In this tutorial I'll introduce you to several of my favorite SPL iterators, providing you with the basis from which you can continue your own exploration. Following several examples, I'll conclude with a brief introduction to other key SPL features.

He looks at how to use the ArrayIterator, DirectoryIterator, SimpleXMLIterator and mentions some of the other handlers for things like advanced data structures, exceptions and more file handling.

tagged: spl standardphplibrary tutorial arrayiterator directoryiterator simplexmliterator

Link:

PHPro.org:
SPL Autoload
Nov 14, 2008 @ 15:31:48

On the PHPro.org website, Kevin Waterson has written up an introduction to the handy autoloading functionality that comes with PHP5 releases - the __autoload method from the Standard PHP Library (SPL).

The SPL __autoload() method is one of the Magic Methods supplied in PHP. The __autoload method is called whenever a class is instantiated and will load the classs the the first time it is called. No longer is include(), require, include_once() or require_once needed as the SPL autoload takes care of this interally.

He looks at how you can use it to load a directory of classes, how to use multiple autoloads in a single script and how to use it with interfaces to register a loader and include it as needed.

tagged: spl autoload standardphplibrary tutorial introduction directory register multiple

Link:


Trending Topics: