<?xml version="1.0"?>
<rss version="2.0">
  <channel>
    <title>PHPDeveloper.org</title>
    <link>http://www.phpdeveloper.org</link>
    <description>Up-to-the Minute PHP News, views and community</description>
    <language>en-us</language>
    <pubDate>Wed, 22 May 2013 22:50:09 -0500</pubDate>
    <ttl>30</ttl>
    <item>
      <title><![CDATA[Setfive.com: PHP: Some thoughts on using array_* with closures]]></title>
      <guid>http://www.phpdeveloper.org/news/19331</guid>
      <link>http://www.phpdeveloper.org/news/19331</link>
      <description><![CDATA[<p>
On the Setfive site there's a recent post from <i>Ashish Datta</i> about <a href="http://shout.setfive.com/2013/03/18/php-some-thoughts-on-using-array_-with-closures/">some thoughts around array functions and closures</a> for callback methods.
</p>
<blockquote>
The other day, I was hacking away on the PHP backend for the "Startup Institute" visualization and I realized it was going to need a good deal of array manipulation. Figuring it was as good a time as any, I decided to try and leverage PHP 5.3+ new closures along with the array_* functions to manipulate the arrays. I'm not well versed with functional programming but I've used Underscore.js's array/collection functions so this is mostly in comparison to that.
</blockquote>
<p>
He gives a sample data set he's pulling from - basic user data - and goes through a few different actions that can be taken on the data (with code examples for each): sorting, mapping and filtering. He shows the use of closures as the callback methods instead of defining them separately and passing in their names. 
</p>]]></description>
      <pubDate>Tue, 19 Mar 2013 10:36:22 -0500</pubDate>
    </item>
    <item>
      <title><![CDATA[Andrew Podner: Closures, Lambdas, and Anonymous Functions]]></title>
      <guid>http://www.phpdeveloper.org/news/19217</guid>
      <link>http://www.phpdeveloper.org/news/19217</link>
      <description><![CDATA[<p>
<i>Andrew Podner</i> has <a href="http://unassumingphp.com/closures-lambdas-and-anonymous-functions/">posted an introduction</a> to a concept in PHP that may not be easy to immediately grasp for someone relatively new to the language - the idea of lambdas and closures.
</p>
<blockquote>
Beginning with PHP 5.3, the anonymous function, became available to developers as a means of improving flexibility and increasing the number of tools at our fingertips to build powerful PHP applications.  What I have been seeing a lot of as I read through documentation is more frequent use of terms like lambda, closure,  or anonymous function, and sometime the terms are often used interchangeably, so let's take a deeper look into  what all of these things are and what differences, if any, there are between them.
</blockquote>
<p>
The post looks at lambdas first, defining them as "functions without names" that can be assigned to objects or called inline when needed. Next is closures, and it's noted that, in PHP, there's really not much difference. They both also allow for use of the "use" statement to import values into the function's local scope. He mentions the "$this" update that came in PHP 5.4 and includes some arguments against their use too.
</p>]]></description>
      <pubDate>Thu, 21 Feb 2013 13:02:13 -0600</pubDate>
    </item>
    <item>
      <title><![CDATA[Vance Lucas: Handling Exceptions in Gearman Tasks (Even Background Ones)]]></title>
      <guid>http://www.phpdeveloper.org/news/18314</guid>
      <link>http://www.phpdeveloper.org/news/18314</link>
      <description><![CDATA[<p>
<i>Vance Lucas</i> has a quick new post to his site showing you how to <a href="http://www.vancelucas.com/blog/handling-exceptions-in-gearman-tasks-even-background-ones/">handle exceptions in Gearman tasks</a> so that they can be logged correctly as a failure.
</p>
<blockquote>
I recently had some issues with Gearman tasks throwing exceptions and killing the whole Gearman daemon. This made it nearly impossible to trace errors back to their origin, because the logged exception stack trace didn't provide much useful information, because it just logged where it failed in Gearman. [...] The only other place to add code that will catch exceptions for all jobs run is in the GearmanWorker::addFunction method. 
</blockquote>
<p>
To solve the issue, he ends up passing in a closure that takes in the $task and wraps its execution in a try/catch to handle the exception correctly. This is then thrown to a custom exception handler and logged for future diagnosis.
</p>]]></description>
      <pubDate>Fri, 03 Aug 2012 08:28:25 -0500</pubDate>
    </item>
    <item>
      <title><![CDATA[BinaryTides.com: Javascript style object literals in PHP]]></title>
      <guid>http://www.phpdeveloper.org/news/18270</guid>
      <link>http://www.phpdeveloper.org/news/18270</link>
      <description><![CDATA[<p>
On the BinaryTides.com site there's a recent post for those familiar with Javascript and wanting to have the same kind of <a href="http://www.binarytides.com/blog/javascript-style-object-literals-in-php/">object-style literals in PHP</a>.
</p>
<blockquote>
As similar thing [to Javascript object literals] can be done in PHP using anonymous functions (since 5.3) [...] Since version 5.3 Php added support for closures and that feature has been used above to create javascript style object literals. The use(&$a) expression makes the variable $a available inside the function happy.
</blockquote>
<p>
Code snippets are included showing how to create these objects via the use of closures and a little trickery with <a href="http://php.net/call_user_func_array">call_user_func_array</a> and __call to make assigning properties easier.
</p>]]></description>
      <pubDate>Wed, 25 Jul 2012 10:44:25 -0500</pubDate>
    </item>
    <item>
      <title><![CDATA[PHP Tip-a-Day: PHP Tutorial: Convoluted Code - Combining Ternary Operators and Anonymous Functions]]></title>
      <guid>http://www.phpdeveloper.org/news/18023</guid>
      <link>http://www.phpdeveloper.org/news/18023</link>
      <description><![CDATA[<p>
On the PHP Tip-a-Day site <i>Greg Bulmash</i> shares a bit of "convoluted code" that could potentially cause confusion in the future maintenance of your application - <a href="http://www.php-tip-a-day.com/php-tutorial-convoluted-code-combining-ternary-operators-and-anonymous-functions/">combining ternary with anonymous functions</a>.
</p>
<blockquote>
Following on yesterday's post about chaining if statements without brackets on a single line, I tried to explore other ways to perform this "test if the variable is set, then do a comparison if it is" logic. I created one of the most convoluted lines of code I've ever written. It's no SQL join that spans 5 whiteboards, but it's pretty unreadable.
</blockquote>
<p>
His example uses not just one ternary comparison, but nested ones with the anonymous function as the first condition. He points out that, if you're not careful with this method and make both sides anonymous functions, you could be in for a "cannot be converted to string" error on the closure side. 
</p>
<blockquote>
I'm sure there might be a very good reason to put two anonymous functions in a ternary operator, but I can't think of one at the moment. It's a fairly ugly proposition.
</blockquote>]]></description>
      <pubDate>Wed, 30 May 2012 10:09:40 -0500</pubDate>
    </item>
    <item>
      <title><![CDATA[DevShed: PHP Closures as View Helpers: Lazy-Loading File Data]]></title>
      <guid>http://www.phpdeveloper.org/news/17466</guid>
      <link>http://www.phpdeveloper.org/news/17466</link>
      <description><![CDATA[<p>
In the second part of their look at using closures in PHP as view helpers, DevShed improves upon <a href="http://phpdeveloper.org/news/17446">their original code</a> by adding some additional classes and <a href="http://www.devshed.com/c/a/PHP/PHP-Closures-as-View-Helpers-LazyLoading-File-Data/">using them in the closures</a>.
</p>
<blockquote>
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.
</blockquote>
<p>
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.
</p>]]></description>
      <pubDate>Mon, 30 Jan 2012 13:08:28 -0600</pubDate>
    </item>
    <item>
      <title><![CDATA[DevShed: Using Closures as View Helpers]]></title>
      <guid>http://www.phpdeveloper.org/news/17446</guid>
      <link>http://www.phpdeveloper.org/news/17446</link>
      <description><![CDATA[<p>
New on DevShed today there's a tutorial looking at using one of the newer features of PHP, closures, <a href="http://www.devshed.com/c/a/PHP/Using-PHP-Closures-as-View-Helpers/">as view helpers</a> in a basic templating system.
</p>
<blockquote>
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.
</blockquote>
<p>
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.
</p>]]></description>
      <pubDate>Wed, 25 Jan 2012 09:50:38 -0600</pubDate>
    </item>
    <item>
      <title><![CDATA[Davey Shafik's Blog: The Closure Puzzle]]></title>
      <guid>http://www.phpdeveloper.org/news/17397</guid>
      <link>http://www.phpdeveloper.org/news/17397</link>
      <description><![CDATA[<p>
<i>Davey Shafik</i> has <a href="http://daveyshafik.com/archives/32789-the-closure-puzzle.html">posted about an interesting find with closures</a> in PHP revolving around an update to add "$this" access inside the closure.
</p>
<blockquote>
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.
</blockquote>
<p>
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 <a href="https://gist.github.com/1607647">included code</a> to illustrate 
 the breakage it can cause in the PHP OOP model (with an explanation).
</p>]]></description>
      <pubDate>Mon, 16 Jan 2012 09:52:38 -0600</pubDate>
    </item>
    <item>
      <title><![CDATA[Jake Smith's Blog: Callback Filter Iterator in PHP 5.3/5.4]]></title>
      <guid>http://www.phpdeveloper.org/news/17202</guid>
      <link>http://www.phpdeveloper.org/news/17202</link>
      <description><![CDATA[<p>
<i>Jake Smith</i> has a new post to his blog today about a feature included in PHP's <a href="http://php.net/spl">Standard PHP Library</a> that you might have overlooked - <a href="http://jakefolio.com/2011/12/callback-filter-iterator-in-php/">the FilterIterator's callback functionality</a>.
</p>
<blockquote>
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.
</blockquote>
<p>
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 <a href="http://www.php.net/manual/en/class.callbackfilteriterator.php">the PHP manual</a>.
</p>]]></description>
      <pubDate>Fri, 02 Dec 2011 08:44:34 -0600</pubDate>
    </item>
    <item>
      <title><![CDATA[Ryan Gantt's Blog: Anonymous recursion in PHP]]></title>
      <guid>http://www.phpdeveloper.org/news/16705</guid>
      <link>http://www.phpdeveloper.org/news/16705</link>
      <description><![CDATA[<p>
In a recent post to his blog <i>Ryan Gantt</i> looks at an interesting way to get around a limitation in PHP dealing with <a href="http://zuttonet.com/articles/anonymous-recursion-php/">anonymous recursion and closures</a> that throws a Fatal error when called.
</p>
<blockquote>
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.
</blockquote>
<p>
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 <a href="http://php.net/array_map">array_map</a> function and another in an <a href="http://php.net/array_reduce">array_reduce</a>.
</p>]]></description>
      <pubDate>Thu, 11 Aug 2011 10:55:35 -0500</pubDate>
    </item>
  </channel>
</rss>
