Jeśli jesteś właścicielem tej strony, możesz wyłączyć reklamę poniżej zmieniając pakiet na PRO lub VIP w panelu naszego hostingu już od 4zł!

DZone.com: Reuse your closures with functors

DZone.com: Reuse your closures with functors

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.

Source: http://www.phpdeveloper.org/news/15651

<!–
var d = new Date();
r = escape(d.getTime()*Math.random());
document.writeln('’);
//–>

Posted Grudzień 31st, 2010 in WEB and PHP Development.

Leave a response: