 | News Feed |
 | Jobs Feed |
Sections
|
| feed this: |  |
PHPMaster.com: List Files and Directories with PHP
by Chris Cornutt October 23, 2012 @ 08:56:25
On PHPMaster.com there's a new tutorial showing you how to work with files and directories through your PHP applications.
In this article I'll talk about a common task you might have experienced while developing a PHP application: listing files and directories. I'll discuss several basic and advanced solutions, each having its pros and cons. First I'll present three approaches that use some very basic PHP functions and then progress to more robust ones which make use of SPL Iterators.
The solutions they look at are the built-in functions like glob and readdir/opendir as well as SPL iterators up for the task - FilesystemIterator, RecursiveDirectoryIterator and GlobIterator. Code samples are included in the post, showing how to use each method to get and list the files. A few helpful hints are also included to finish off the tutorial (mostly about "tricks" to using the functions effectively).
voice your opinion now!
tutorial file directory list spl iterator
Web Mozarts: Give the Traversable Interface Some Love
by Chris Cornutt October 10, 2012 @ 09:12:56
In this recent post to the Web Mozarts site, Bernhard Schussek "gives Traversable some love" and introduces you to the Traversable interface and how it might work better for certain things than an Iterator.
Let's start with a simple use case. Let's create an interface ArrayInterface that demarcates objects that behave like PHP arrays. The interface should allow for counting, iterating and array access.
He shows how to create this interface based off of a "ArrayInterface" that implements "Countable", "ArrayAccess" and "Iterator" with all of the methods required for each. He points out that, while the documentation in the manual makes "Traversable" shouldn't be used, it can be extended instead of Iterator. This gives other classes that extend this interface the option of extending either of the Iterators ("Iterator" or "IteratorAggregate") they want.
voice your opinion now!
interface traversable introduction iterator iteratoraggregate
Thomas Weinart: What Iterators Can Do For You
by Chris Cornutt August 01, 2012 @ 09:55:22
Thomas Weinert has a new post to his site showing some of the things that iterators can do for you (including working with arrays and aggregation).
Basically Iterators provide a list interface for an object. Like all interfaces they are a contract how something can be used. If you use an interface it is not relevant how it is implemented - the implementation logic is encapsulated. It is of course relevant on the integration level. A bad implementation can impact the performance of you application. Even an good implementation may need special resources (like a database). But all this does not impact how you use it. Your code using the object with the Iterator interface stays the same.
He shows how to use the IteratorAggregate, ArrayIterator, FilterIterator and how to create a custom Iterator that you can extend in your own code.
voice your opinion now!
iterator tutorial array filter aggregate custom
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
PHPMaster.com: Using SPL Iterators, Part 2
by Chris Cornutt May 22, 2012 @ 08:23:17
On PHPMaster.com today they've posted the second part of the series covering the Iterators that come with PHP as a part of the SPL.
In part one of this series I introduced you to some of the SPL Iterators and how to use them. There may be times however when the provided iterators are insufficient for your needs and you'll want to roll your own custom iterator. Luckily, SPL provides interfaces that will allow you to do just that. For an object to be traversable in a foreach loop, PHP requires that it be an instance of Traversable. You cannot however implement this interface directly (though you can use it in instaceof checks); instead you'll need to implement either SPL's Iterator or IteratorAggregate interfaces.
He shows you how to implement these two interfaces in your own custom classes, looping through a set of books for the Iterator example and a "getIterator" method that creates an ArrayIterator when executed. The results of both are used in foreach loops showing how they can be used just like any other iteratable variables.
voice your opinion now!
spl iterators tutorial array iterator iteratoraggregate foreach
Jeremy Cook's Blog: Implementing IteratorAggregate and Iterator
by Chris Cornutt May 14, 2012 @ 13:04:58
In a recent post to his blog Jeremy Cook has gotten back into looking at some of the SPL functionality that comes with PHP. In this new post he looks specifically at the IteratorAggregate and Iterator object types.
After a bit of a break I'm finally able to get back to writing about the predefined interfaces in PHP. PHP provides two interfaces that allow you to define how your objects behave in a foreach loop: IteratorAggregate and Iterator. Before taking a look at IteratorAggregate I'll briefly discuss how we can iterate over objects in PHP 'natively' and what it means to be Traversable.
He introduces the concepts being being "iteratable" and "traversable". He then shows how to implement the IteratorAggregate (only one method required, "getIterator") and Iterator ("next", "valid", "current" and "key" methods required) in classes of your own.
You can find out more about these two object types (including more sample usage) on their manual pages: IteratorAggregate & Iterator.
voice your opinion now!
iteratoraggregate iterator tutorial iterate traversable
Stefan Koopmanschap's Blog: GlobIterator Easy access to specific files
by Chris Cornutt April 17, 2012 @ 12:43:42
Stefan Koopmanschap has a new post to his blog showing a handy use of the GlobIterator to access only certain files.
For a project I am working on I needed to iterate over all .xml files in a specific directory. I started out with a DirectoryIterator, then considered I didn't want the XML filtering to take place inside my foreach loop. I decided to add a FilterIterator to the setup, but then felt this was not the right solution either. So I turned to my favorite SPL guru, Joshua Thijssen, to see if I was overseeing some kind of filter-option in the DirectoryIterator. I didn't, but I did oversee something else: GlobIterator.
The GlobIterator lets you use functionality similar to the glob function (including being able to use wildcards in file searching) and get the resulting list back as a set of SplFileInfo objects, complete with additional metadata that can be extracted.
voice your opinion now!
globiterator spl iterator glob filesystem find
Lorna Mitchell's Blog: Using iterator_to_array() in PHP
by Chris Cornutt February 29, 2012 @ 08:55:52
Lorna Mitchell has a new post to her blog today showing off a lesser-known but very useful function included in PHP - the iterator_to_array function, used to translate things that implement Traversable into arrays.
Someone watching over my shoulder recently had never seen the ubiquitously-useful iterator_to_array() before. [...] Mostly I find this useful when I'm working with collections of data as these often present themselves as an object that you can foreach() over, but you can't dump it directly. If the object in question implements the Traversable interface, you can instead pass it into iterator_to_array to get the data as an array.
She includes a brief snippet of code showing it in use - transforming the results from a MongoDB cursor object back into an array.
voice your opinion now!
iterator translate array snippet traversable
|
Community Events
Don't see your event here? Let us know!
|