 | News Feed |
 | Jobs Feed |
Sections
|
| feed this: |  |
Giorgio Sironi's Blog: Stop writing foreach() cycles
by Chris Cornutt February 18, 2010 @ 10:58:28
Giorgio Sironi has a recommendation for developers out there - stop writing foreach loops, there's something better in PHP 5.3+ - closures
There are some array functions which have already been supported at least from Php 4, and that take as an argument a callback whose formal parameters have to be one or two elements of the array. [...] In Php 5.3, callbacks may also be specified as anonymous functions, defined in the middle of other code. These closures are first class citizens, and are treated as you would treat a variable, by passing it around as a method parameter.
He includes some code examples to show you how closures used in callbacks can replace a lot of the other looping normally done by a separate bit of code. Most of the instances are in array functions that take in a callback and apply it to each element in the array (some recursively). The last example shows how to use it in a usort call to make the custom sorting of an array simpler.
voice your opinion now!
foreach closure tutorial array callback
Vance Lucas' Blog: Get Only Public Class Properties for the Current Class in PHP
by Chris Cornutt January 06, 2010 @ 10:06:29
On his blog today Vance Lucas has posted a method you can use to only get the properties of your class that are in the "public" scope.
PHP provides two built-in functions to retrieve properties of a given class '" get_object_vars and get_class_vars. Both these functions behave the same exact way, one taking an object as a variable and the other taking a string class name. The tricky thing about the two functions is that they behave differently depending on the call scope, returning all of the class variables available within the called scope.
As a bit of a hack (in lower than PHP 5.3) he shows how to use the create_function function to create a small statement in a different scope that returns the only the variables seen from the "outside" - just the public ones. PHP 5.3 users can do it much more cleanly with closures. Code examples for both are included.
voice your opinion now!
public class property createfunction closure tutorial
Pawel Turlejski's Blo: What's wrong with PHP closures?
by Chris Cornutt October 06, 2009 @ 13:16:18
In a recent post Pawel Turlejski takes a look at what he thinks is wrong with PHP closures as compared to syntax in a few other languages.
PHP 5.3, along with many other features, introduced closures. So now we can finally do all the cool stuff that Ruby / Groovy / Scala / any_modern_language guys can do, right? Well, we can, but we probably won't... Here's why.
He compares the PHP syntax for using closures/lambda functions with the abilities of Groovy and Scala. He does point out out that the ArrayObject wrapper does allow you to work a bit more fluently with the array's contents, but it's still not quite the same. According to him:
I'm sure closures will find their uses in the PHP world (like delayed execution or automated resource management), but IMHO replacing traditional loops and array operations is not one of them.
voice your opinion now!
closure groovy scala array example
Stubbles Blog: Extending objects with new methods at runtime
by Chris Cornutt September 01, 2009 @ 09:05:46
On the Stubbles blog today Frank Kleine looks at a cool trick that the features of PHP 5.3 now make possible - extending objects at runtime by adding in new methods and functionality.
With the advent of PHP 5.3 adding new methods to an instance of a class at runtime becomes possible with PHP as well, using anonymous functions and a little bit of __call() magic.
He illustrates with a bit of code - defining the base class and using a __call method to catch any undefined method calls. Using this in conjunction with closures makes it possible to call the method internally, avoiding the usual error that could result from setting it like you would a class property.
voice your opinion now!
extend object closure call
Fabien Potencier's Blog: On PHP 5.3, Lambda Functions, and Closures
by Chris Cornutt April 17, 2009 @ 10:29:43
In this new post to his blog Fabien Potencier looks at two of the much-hyped features of the upcoming PHP 5.x series release (5.3) - closures and lambda functions.
I won't talk too much about what lambda functions or closures are, as you can find many good blog posts describing them in great details. To sum up, a lambda function is an anonymous PHP function that can be stored in a variable and passed as an argument to other functions or methods. A closure is a lambda function that is aware of its surrounding context.
He includes several examples including how they would work with a few of the array functions, an implementation of the Y-combinator method (as written by Stanislav Malyshev) and how they can be used to create dependency injection functionality.
voice your opinion now!
lambda function closure php5 array ycombinator dependency injection container
PHP 10.1 Blog: Y-Combinator in PHP
by Chris Cornutt 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.
voice your opinion now!
ycombinator closure php5 recursive
|
Community Events
Don't see your event here? Let us know!
|