News Feed
Sections




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

DZone.com:
Reuse your closures with functors
December 29, 2010 @ 10:50:19

On DZone.com there's a new tutorial from Giorgio Sironi about reusing closures with the help of functors (a special kind of object instancing done in PHP with the help of __invoke).

I like PHP closures and their superset, anomyous functions, as they implement the Command pattern very well when used correctly. However I feel that sometimes they are: difficult to reuse and difficult to force contracts on. [...] What if we wanted instead, a closure which we can instance even more than one time (maybe with different variables), and that we could type hint?

He shows how to make this possible with a functor created using the __invoke magic method of PHP to handle the request to an object like a function. He includes some sample code to show it in action - a basic callback (SquareCallback) and how it compares to calling a normal closure. It also shows something a bit more technical, an "AdderCallback" class that can be defined as a type hint on a function.

0 comments voice your opinion now!
closure functor invoke snippet example callback typehint


blog comments powered by Disqus

Similar Posts

Chris Roane's Blog: The Last Few Years: What a PHP Programmer Has Learned

John Maver's Blog: How to create a Bebo application using PHP 5

Padraic Brady's Blog: Zend Framework Blog Tutorial - Part 7: Authorisation with Zend_Acl & Styling

Maarten Balliauw's Blog: Reuse Excel business logic with PHPExcel

Chris Hartjes' Blog: The Devilís In The Details (or, Handling Sequences in Postgres in CakePHP)


Community Events





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


release voicesoftheelephpant list threedevsandamaybe language tips series api code introduction symfony framework community bugfix zendserver laravel deployment conference interview podcast

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