 | News Feed |
 | Jobs Feed |
Sections
|
| feed this: |  |
DevShed: PHP Closures as View Helpers Lazy-Loading File Data
by Chris Cornutt January 30, 2012 @ 13:08:28
In the second part of their look at using closures in PHP as view helpers, DevShed improves upon their original code by adding some additional classes and using them in the closures.
The best way to show you how using anonymous functions can help you to develop more efficient OO applications is with some functional, hands-on examples. With this idea in mind, in the installment that preceded this one, I implemented an extendable template system. This system could spawn view objects and render the template files associated with these objects.
In this second part of the (two-part) series they include "Serializer" and "FileHandler" classes and show how to use them inside of the closures to lazy-load in data from an external file and work with it as serialized content.
voice your opinion now!
tutorial closure lazyload file serialize view helper
DevShed: Using Closures as View Helpers
by Chris Cornutt January 25, 2012 @ 09:50:38
New on DevShed today there's a tutorial looking at using one of the newer features of PHP, closures, as view helpers in a basic templating system.
In this two-part tutorial I'll be showing you, in a step-by-step fashion, how to use the goodies offered by closures in the implementation of an object-based, easily extendable template system. This system will allow you to embed anonymous functions easily into template files, and call them as typical view helpers, too.
He starts the process of creating the templating system by defining two interfaces, the View and DataHandler. Using these as a base, he creates an instance of the ViewInterface (a "View" class) that can set the template file to use, set values to be displayed and render the formatted output. Included is a basic template and how to use the View class to set values into it. The "render" method is called on the view and the HTML markup is produced. The closure comes in when they try to call a value "clientIp" that needs to do something more complicated than just having a string assigned to it.
voice your opinion now!
closure view helper template view interface
Davey Shafik's Blog: The Closure Puzzle
by Chris Cornutt January 16, 2012 @ 09:52:38
Davey Shafik has posted about an interesting find with closures in PHP revolving around an update to add "$this" access inside the closure.
However, it didn't stop there; there was also the addition of Closure::bind() and Closure->bindTo(). These methods are identical except one is a static method into which the closure is passed, the second an instance method on the closure itself. These methods both take two arguments (on top of the closure for the static version): $newthis and $newscope. What this means is that unlike the regular object model the concept of $this and lexical scope (what is in scope for the function with regards to private/protected methods inside objects) are completely separated.
He also mentions that you can change the "$this" to a different object (complex) or swapping out the object the closure is bound to while keeping "$this" the same (simpler). He mentions that it could be useful for unit testing but can have its drawbacks. He's included code to illustrate
the breakage it can cause in the PHP OOP model (with an explanation).
voice your opinion now!
closure puzzle bindto bind oop object
Jake Smith's Blog: Callback Filter Iterator in PHP 5.3/5.4
by Chris Cornutt December 02, 2011 @ 08:44:34
Jake Smith has a new post to his blog today about a feature included in PHP's Standard PHP Library that you might have overlooked - the FilterIterator's callback functionality.
The Filter Iterator is probably my second favorite iterator, next to Directory Iterator. There are many great use cases for the Filter Iterator, and when you do filter the original data is left untouched. A Filter Iterator is really simple to use, create a class that extends FilterIterator and adjust the accept method to meet your criteria. This is great and all, but having the ability to create filter iterators on the fly, ones that won't be used application wide, without having to create a class is even better.
He includes a bit of code defining a FilterCallbackIterator class with a "callback" parameter passed into the constructor (in his case, a closure). Also included is some sample code of it in use - handling an array (well, ArrayIterator) with a simple true/false check on the current array value. You can find out more about this functionality in the PHP manual.
voice your opinion now!
callback filter iterator spl tutorial closure
Ryan Gantt's Blog: Anonymous recursion in PHP
by Chris Cornutt August 11, 2011 @ 10:55:35
In a recent post to his blog Ryan Gantt looks at an interesting way to get around a limitation in PHP dealing with anonymous recursion and closures that throws a Fatal error when called.
Turns out that variables called as functions must be an instance of Closure, an instance of a class which implements __invoke(), or a string representing a named function in the global namespace. In the anonymous function body above, $fibonacci is none of these. It is an undeclared, free variable in the closure created by the anonymous function. At the time when it's called, it hasn't been bound-hence the Notice that you would have gotten if error reporting were set at a high enough threshold - and therefore can't be called as anything, let alone as a function.
He tried using the "use" functionality PHP closures have to bring a variable/object/etc into the scope of the running function, but it still threw an error. As it turns out, the combination of "use"-ing the object and calling it by reference handles things correctly. He takes this method and applies it in two examples - one call in an array_map function and another in an array_reduce.
voice your opinion now!
anonymous recursion reference invoke closure
Stoyan Stefanov's Blog: JavaScript-style object literals in PHP
by Chris Cornutt March 25, 2011 @ 09:02:59
Stoyan Stefanov has a new post to his blog looking at bringing something to PHP that Javascript developers have always had - object literals. These literals allow you to assign object properties at definition time instead of the usual PHP method of adding them either in the class definition or after the object exists.
Stoyan starts with an example of the object literals on Javascript and shows how it's (sort of) possible to replicate that wit type casting an array to an object in PHP. The tricky part comes in when you try to recreate the method handling JS gives in objects. Closures in PHP 5.3 comes close, but there's a few things he points out that just won't work the same way.
You can simulate some of this with __call method handling in a base "JSObject" class. For more details on his work bringing Javascript over to the world of PHP, check out his presentation from this year's ConFoo conference.
voice your opinion now!
javascript object literals tutorial closure call
Fabian Schmengler's Blog: Anonymous function calls in PHP
by Chris Cornutt February 25, 2011 @ 09:52:27
Fabian Schmengler has a new post today looking a using anonymous function calls in PHP. He relates to to another popular language that allows for dynamic anonymous functions - Javascript.
Anonymous function calls are a well-known pattern in JavaScript but there are also use cases in PHP where they make sense. Of course PHP 5.3 with its Lambda Functions is required!
He includes several little code snippets showing how the anonymous functions work including the "use" keyword functionality that lets you import variables from outside the function. There's a sneaky pass-by-reference in there, so don't get tripped up.
voice your opinion now!
anonymous function closure introduction tutorial
Lorna Mitchell's Blog: Callbacks in PHP
by Chris Cornutt February 14, 2011 @ 13:41:28
Lorna Mitchell has a new post to her blog today looking at a very handy piece of PHP functionality sprinkled around in different functions - using callbacks to handle complicated processing.
Recently I was working on something and I wanted to call an object method as a callback, but got confused when I realised the method had been caused statically. This was caused by my inability to RTFM and I wondered how I'd come so far without actually coming across the many and varied things you can pass into any place a callback is needed.
Besides the normal callback functions you can put in something like call_user_func, she also mentions something a bit more powerful - passing in an array that contains a pointer to an object and a method inside it. This ability allows you to keep your OOP encapsulation intact without having to make global functions. In PHP 5.3, there's even some of the PHP functions that use call backs that will allow you to use closures/anonymous functions without even having to make a separate function.
voice your opinion now!
callback calluserfunc function object closure anonymous
Shameer's Blog: PHP 5.3 Practical look into Lambda functions and closures
by Chris Cornutt December 30, 2010 @ 11:11:26
For those out there still coming to grips with some of what PHP 5.3 has to offer, I'd suggest checking out this new article from Shameer about using lambdas and closures along with some examples putting them into action.
PHP 5.3 packaged the power of functional programming by adding support for lambda functions and closures. You will be familiar with them in javascript. In this article we will have a look into these features and its usages.
He starts with explanations of what they are with simple code examples showing their use - a lambda assigned to a variable and closures with the "use" keyword. He shows how they can be used in application prototyping, making handy callback functions and includes a practical example of finding the factorial of a number using a lambda.
voice your opinion now!
lambda closure tutorial practical example
|
Community Events
Don't see your event here? Let us know!
|