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

SitePoint PHP Blog:
Laravel Blade Recursive Partials with @each
Apr 02, 2015 @ 13:21:50

On the SitePoint PHP blog there's a post from editor Bruno Skvorc showing you how to create recursive partials in Blade, the templating library that the Laravel framework uses internally for rendering output.

n this tutorial, we’ll go through the process of implementing recursive partials in Laravel’s Blade templating engine by means of the @each command. This will allow us to render data structures with an arbitrary number of nested children without needing to know the maximum depth of the array.

In his example, he's rendering the data from a nested set, a folder structure that could potentially go many levels down. He gives an example of the data he's working with in PHP arrays and how it could be outputted in plain old PHP. Of course, things have to be done a little differently in Blade and he includes the templates to do it - the main level and a partial that's used to output the folder information. He shows the use of "@each" in these examples and explains how it works and an example of the output.

tagged: laravel blade recursive child template tutorial example

Link: http://www.sitepoint.com/laravel-blade-recursive-partials/

PHPMaster.com:
Using SPL Iterators, Part 1
May 15, 2012 @ 17: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".

tagged: spl iterators tutorial array directory file recursive

Link:

PHPBuilder.com:
PHP Arrays: Advanced Iteration and Manipulation
Dec 09, 2011 @ 18: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.

tagged: array manipulation advanced iteration spl recursive sort

Link:

Jeremy Cook's Blog:
Recursive Closures in PHP 5.3
Aug 13, 2010 @ 18: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.

tagged: closure recursive stripslashes tutorial

Link:

Rafael Dohms' Blog:
Iterating over life with SPL Iterators I: Directories
Oct 08, 2009 @ 16: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.

tagged: spl iterator directory recursive

Link:

DevShed:
Using Directory Iterators to Build Loader Apps in PHP
Jul 06, 2009 @ 15: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.

tagged: tutorial autoload recursive iterator directory spl

Link:

DevShed:
Developing a Recursive Loading Class for Loader Applications in PHP
Jun 18, 2009 @ 15: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.

tagged: loader recursive tutorial

Link:

DevShed:
Including Files Recursively with Loader Applications in PHP
Jun 11, 2009 @ 17: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.

tagged: loader recursive tutorial include

Link:

PHP 10.1 Blog:
Y-Combinator in PHP
Apr 14, 2009 @ 16: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.

tagged: ycombinator closure php5 recursive

Link:

Builder.com.au:
How do I...recursively scan directories with PHP's DirectoryIterators?
Jun 18, 2008 @ 12: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.

tagged: directoryiterator iterator recursive directory example

Link:


Trending Topics: