News Feed
Jobs Feed
Sections




News Archive
Devis Lucato's Blog:
Anonymous objects in PHP - Composition, Mocks, Refactoring
November 23, 2010 @ 13:17:53

In a new post to his blog Devis Lucato points out something he noticed when working with objects and anonymous functions/closures - they're not all as they seem.

Both solutions allow to instantiate an anonymous object with properties. They are used as value objects and have no other purpose than storing values, so no logic can be included and they don't come with methods. They can be used as function parameters instead of arrays, for instance. PHP 5.3.0 introduced anonymous functions and closures, so it is now possible to attach functions to these VOs (*). [...] The first thing to notice is that these properties are not methods but callable functions:

In his example, an anonymous function dynamically appended to an object doesn't have access to a property set on the object just one line before. There's a way around it with call_user_func, but it's not practical. His proposed solution is to create a type of Anonymous class that uses the __call method to catch the methods and translate them into calls to call_user_func_array automatically.

0 comments voice your opinion now!
anonymous objects composition mocking refactoring


blog comments powered by Disqus

Similar Posts

IBM developerWorks: SDO for PHP Demonstrations from php|tek

Zend Developer Zone: Book Review: Learning PHP Data Objects

Bence Eros' Blog: Using Inheritance

DevShed: Collections and Sorting

Soledad Penades' Blog: Signs your PHP needs refactoring


Community Events









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


example community zendframework2 opinion introduction language event functional api tool interview testing release unittest development composer code framework podcast object

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