 | News Feed |
 | Jobs Feed |
Sections
|
| feed this: |  |
Anthony Ferrara: What Generators Can Do For You
by Chris Cornutt July 24, 2012 @ 08:43:58
Anthony Ferarra has a new post looking at using generators in your code (as recently proposed for addition in PHP's core (Possibly for 5.5.0). While I believe that this is a great tool, it appears that many PHP developers aren't familiar with the concept of generators. So I thought I would take a little time and explain some of how it works, and how it can be used to greatly simplify code.
He explains the concept of "generators" as an easier way to implement iterators. In his example he shows how to refactor is file handling iterator to replace it with generator functionality. It uses a new keyword, "yield", to return a Generator instance that can then can be used much like the file iterator without the need for all of the code to create the iterator itself. His more complex example shows how to replace an ArrayObject instance by a little trick inside its "getIterator" method.
voice your opinion now!
generator proposal rfc tutorial iterator arrayobject
Brandon Savage's Blog: A Closer Look At ArrayObject
by Chris Cornutt April 26, 2010 @ 10:50:33
In a new post to his blog today Brandon Savage has taken a look at the SPL ArrayObject component including how it's handled in a PHP5 OOP kind of world.
ArrayObject is an object that is designed to behave exactly like an array. If that seems confusing, don't worry, it's not. ArrayObject is an object. It follows all the rules of how objects work. But it's designed to implicitly behave like an array for all intents and purposes, including being used in a foreach loop, and accessing it's properties just like you would access the values in an array.
He includes a code snippet showing how the ArrayObject can be used and explains that, since it's an object and not an array, the "copy, not duplicate" handling applies to it. You get the best of two worlds - the ease of an array and the power of an object (plus there's some performance improvements too).
voice your opinion now!
arrayobject spl object array
Havard Eide's Blog: Countable
by Chris Cornutt August 01, 2008 @ 10:23:28
In a new post Havard Eide looks at the creation of a Countable interface that can be used in any application:
Today I will look at the Countable interface, it has a single function that needs to be implemented: count(), by implementing this you can ensure that there is a count() function ready to use on any given class that implements it. The Countable interface is used in other places in the SPL as well: the ArrayIterator and ArrayObject classes implements this interface ( and SqliteResult if present ).
In his code examples he shows simple methods for returning the count() of a property, but notes that the real power of it comes in the ability to manipulate the number returned from the call based on other parameters (or filtering).
voice your opinion now!
countable interface count spl arrayiterator arrayobject
Matthew Weir O'Phinney's Blog: Overloading arrays in PHP 5.2.0
by Chris Cornutt January 19, 2008 @ 08:01:25
In a new post to his blog, Matthew Weir O'Phinney talks about a method for overloading arrays in a script written for the PHP 5.2 series.
Several weeks back, a bug was reported against Zend_View that had me initially stumped. [...] I'd read about this some months back on the php internals list, but at the time hadn't understood the consequences.
Basically, __get() no longer returns a reference and returns values in read mode, which makes modifying arrays using overloading impossible using traditional methods.
Unfortunately, this was exactly the functionality that was needed, so Matthew set out to find a way to do just that. His initial method, extending the ArrayObject, worked but still gave errors. On Mike Naberezny's recommendation, though, this too was resolved with a simple call to the __set method instead.
voice your opinion now!
overload array arrayobject set get reference overload array arrayobject set get reference
|
Community Events
Don't see your event here? Let us know!
|