 | News Feed |
 | Jobs Feed |
Sections
|
| feed this: |  |
Jeremy Cook's Blog: Implementing the ArrayAccess Interface
by Chris Cornutt February 02, 2012 @ 13: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.
voice your opinion now!
arrayaccess spl tutorial array standardphplibrary countable
Jeremy Cook's Blog: Using the Countable Interface
by Chris Cornutt January 05, 2012 @ 14: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).
voice your opinion now!
spl standardphplibrary countable interface tutorial
Jani Hartikainen's Blog: How to use built-in SPL exception classes for better error handling
by Chris Cornutt May 09, 2011 @ 08: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.
voice your opinion now!
spl standardphplibrary exception handling classes types
PHPro.org: SPL Autoload
by Chris Cornutt November 14, 2008 @ 09: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.
voice your opinion now!
spl autoload standardphplibrary tutorial introduction directory register multiple
Etienne Kneuss' Blog: SPL Datastructures updated
by Chris Cornutt May 13, 2008 @ 09:31:24
Etienne Kneuss has posted about some updates to the data structures functionality in the Standard PHP Library (SPL), specifically some new additions.
There finally is documentation for SplDoublyLinkedList, SplStack and SplQueue and some "new" classes: SplHeap (abstract), SplMaxHeap, SplMinHeap and SplPriorityQueue, documentation of those classes is in progress.
An example of the new functionality (for SplPriorityQueue) is included in the post showing the insertion of a value into a pre-existing array (without having to slice or splice!).
voice your opinion now!
spl standardphplibrary datastructure heap priorityqueue
Zend Developer Zone: The Standard PHP Library (SPL)
by Chris Cornutt September 26, 2007 @ 07:52:31
The Zend Developer Zone has posted a new article/tutorial from Ben Ramsey covering one of the more powerful bits of functionality that's included with PHP5 - the Standard PHP Library (SPL).
As its name implies, the goal of the Standard PHP Library-or SPL, for short-is to provide a standard library of interfaces that allows developers to take full advantage of object-oriented programming in PHP 5. [...] The functionality it provides includes, for example, the ability to define how your objects will react when iterated over with foreach, advanced array access, file and directory access, and advanced SimpleXML object handling. The largest chunk of functionality that the SPL provides comes in the form of iterators.
Ben focuses on the iterators - three different kinds: IteratorAggregate, RecursiveIterator and SeekableIterator. The SPL also comes with a few other built-in kinds of iterators he mentions as well:
- ArrayIterator
- DirectoryIterator
- FileIterator
As a bonus, he also talks about other "goodies" the SPL has to offer like exceptions, array handling, and file/directory access.
Also, check out Ben's comments on his own blog about the article.
voice your opinion now!
spl standardphplibrary tutorial iterator goodies array directory file spl standardphplibrary tutorial iterator goodies array directory file
|
Community Events
Don't see your event here? Let us know!
|