News Feed
Sections




News Archive
feed this:

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

PHPMaster.com:
Using SPL Iterators, Part 1
May 15, 2012 @ 12:26:59

On PHPMaster.com today there's a new tutorial posted, the first part of a series, looking at the use of the Standard PHP Library (SPL) in PHP. In this first part of the series, Stefan Froelich looks specifically at two of the more common uses for iterators - working with arrays and directories.

When I first came across the term iteration and saw the overwhelming list of classes related to it in the SPL, I was taken aback. It seemed maybe iteration was too complex for me to grasp. I soon realized it was just a fancy word for something we programmers do all the time. [...] In the first part of this two-part series I'll introduce you to iteration and how you can take advantage of some of the built-in classes from the Standard PHP Library (SPL).

Included in the tutorial is example code showing how to use the ArrayIterator to work with an array and the DirectoryIterator to process the contents of a directory. He also briefly touches on a few other iterators like "FileExtensionFilter", "RecursiveDirectoryIterator" and "RecursiveArrayIterator".

0 comments voice your opinion now!
spl iterators tutorial array directory file recursive


PHPBuilder.com:
PHP Arrays Advanced Iteration and Manipulation
December 09, 2011 @ 12:50:11

In this new tutorial from PHPBuilder.com, Jason Gilmore shows you some of the more advanced things you can do with arrays in PHP (specifically in the areas of iterating through them and manipulating their contents).

Sporting more than 70 native array-related functions, PHP's array manipulation capabilities have long been one of the language's most attractive features. [...] There are however many array-related tasks which ask a bit more of the developer than merely knowing what part of the manual one needs to consult. Many such tasks require a somewhat more in-depth understanding of the native features, or are possible only when a bit of imagination is applied to the problem.

In his examples he shows how to do things like sorting a multi-dimensional array, iterating recursively (with the help of a RecursiveArrayIterator), converting an object to an array and doing "natural" sorting on an array's contents.

0 comments voice your opinion now!
array manipulation advanced iteration spl recursive sort


Jeremy Cook's Blog:
Recursive Closures in PHP 5.3
August 13, 2010 @ 13:07:59

In this recent post from Jeremy Cook he take a look at an interesting use of closures in PHP - using them recursively to strip slashes off a string.

One thing all of my projects have in them is code to remove the quotes added so I can handle appropriate escaping myself. This [example from the PHP manual] works perfectly but it does end up creating a function in the global namespace which is only called once. A perfect job for a closure.

He includes an example of how to do it with a normal, globally-defined function and how to handle it with a closure (that calls itself via an array_map) - a closure contained inside of itself with the string variable passed in by reference. Then, when it's done stripping out slashes, the closure is dropped and nothing new is added to the global namespace.

0 comments voice your opinion now!
closure recursive stripslashes tutorial


Rafael Dohms' Blog:
Iterating over life with SPL Iterators I Directories
October 08, 2009 @ 11:57:33

Rafael Dohms has posted the first part of a series he's writing looking at the Iterators that come with the Standard PHP Library (SPL). In this first article he focuses on Directory iterators.

Wouldn't it be nice if you could go by life just applying a foreach to each year and life day by day? Ok, that was an awful joke, but using iterators does make life a lot easier and fun, and that's without mentioning cleaner code. SPL's iterator classes are really awesome and helpful, replacing multiple lines of code and a handful functions with a simple new this and a foreach can really help cleaning up code.

He looks at both the DirectoryIterator and the RecursiveDirectoryIterator with code examples to go with each.

0 comments voice your opinion now!
spl iterator directory recursive


DevShed:
Using Directory Iterators to Build Loader Apps in PHP
July 06, 2009 @ 10:17:11

DevShed finishes off their "loader" series of tutorials today with this eighth part focusing on the use of Directory Iterators.

Here's where the SPL comes in, since it's possible to use a combination of its "spl_autoload_register()" function and its RecursiveDirectoryIterator class to refactor the method in question and make it shorter and tighter. In this final chapter of the series I'm going to improve the loader class developed in the previous one by incorporating some of the aforementioned SPL functions and classes.

They change up their code to use a RecursiveDirectoryIterator inside of their __autoload to remove their custom recursive code.

0 comments voice your opinion now!
tutorial autoload recursive iterator directory spl


DevShed:
Developing a Recursive Loading Class for Loader Applications in PHP
June 18, 2009 @ 10:27:08

DevShed has posted part three of their series looking at loader applications in PHP using the __autoload magic method.

While this file loading class in its current incarnation does a pretty good job, it lacks an important feature that could be added with relative ease. It cannot perform a recursive search through the web server's file system, and afterward include a specified file within a given application.

This article updates their class with a new feature to let the script looking for files recursively in a directory.

0 comments voice your opinion now!
loader recursive tutorial


DevShed:
Including Files Recursively with Loader Applications in PHP
June 11, 2009 @ 12:43:23

DevShed continues their "loaders in applications" series with this fourth part, a look at including files recursively.

This series uses a variety of code samples to teach you how to create modular programs. These programs are capable of recursively including files required by a given application, without having to explicitly call any "include()/include_once()" or "require()/require_once()" PHP function.

In their example they show how to use their loader class (built up from previous parts of the series) and modify it slightly to allow the script to set the file path, set the files to include and pull them in.

0 comments voice your opinion now!
loader recursive tutorial include


PHP 10.1 Blog:
Y-Combinator in PHP
April 14, 2009 @ 11:17:19

New from the PHP 10.0 blog today is this look at trying to set up recursive closures in the upcoming PHP 5.3 release (it includes closures, but it doesn't look like this is possible). Instead, Stas suggests the Y combinator method as an alternative.

One of the ways to do it is to use Y combinator function, which allows, by application of dark magic and friendly spirits from other dimensions, to convert non-recursive code to recursive code. [...] Doing Y-combinator in PHP was attempted before (and here), but now I think it works better. It could be even nicer if PHP syntax allowed chaining function invocations - ($foo($bar))($baz) - but for now it doesn't.

His (less than ideal) first method throws in some variable variables and a separate factoral function and his second passes in a factoral value of itself to call itself the correct number of times.

0 comments voice your opinion now!
ycombinator closure php5 recursive


Builder.com.au:
How do I...recursively scan directories with PHP's DirectoryIterators?
June 18, 2008 @ 07:55:21

Builder.com.au has a new tutorial posted today talking about the use if Iterators (from PHP's SPL) to recurse down through directories on your local drive.

One of PHP5's most interesting new features is the addition of Iterators, a collection of ready-made interfaces designed to help in navigating and processing hierarchical data structures. These Iterators significantly reduce the amount of code required to process an XML document tree or a file collection.

They give three examples - two basic ones showing a simple use of the DirectoryIterator and RecursiveDirectoryIterator and another slightly more complex one showing how to get information from the recursive iteration as it goes down.

0 comments voice your opinion now!
directoryiterator iterator recursive directory example


Brian Moon's Blog:
Stupid PHP Tricks Normalizing SimpleXML Data
June 03, 2008 @ 09:34:22

Brian Moon has a "stupid PHP trick" posted to his blog today - normalizing SimpleXML data you've pulled in from just about any external source.

Anyhow, one annoying thing about SimpleXML has to do with caching. When using web services, we often cache the contents we get back. We were having a problem where we would get an error about a SimpleXML node not existing.

They were using memcache to store the information but came across problems when their code tried to use a (sometimes) empty tag. He gives two solutions - one using a recursive function that identifies the empty items and the other that encodes then decodes the object to and from JSON, keeping the values intact.

0 comments voice your opinion now!
trick stupid simplexml normalize json recursive empty tag



Community Events





Don't see your event here?
Let us know!


opinion voicesoftheelephpant conference release series laravel interview list package community composer podcast security tool introduction version symfony library framework language

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework