The decorator pattern is commonly described as a pair of classes—a component and a decorator—which derive from a common ancestor. Using language-level features in PHP, we can relax this restriction and still reap the benefits of runtime object-behavior modification. To change the functionality of the component, the decorator redefines one or more methods. Within these […]
Category: articles
Anonymous functions and closure in PHP
PHP 5.3 introduced function literals to the language, providing big benefits in the form of syntax and readability when using and creating functions that accept other functions as arguments. However, the introduction of anonymous functions was paired with another useful language construct which allows us to close over local variables in the same scope as […]
Anonymous recursion in PHP
The scenario You’re neck deep in 3-year old production code, amazon one-click checkout isn’t working, and your wife just left you for someone who “doesn’t spend so much time writing unit tests.” You need to save the world by implementing an algorithm using an anonymous function now. Wait, what if the problem you need to […]
Horizontal reusability with traits
The ability for a class to inherit from multiple parents is maligned by many, but can be a good thing in some situations. For those working in PHP, multiple inheritance has never been an option; classes are limited to one parent, though they can implement many other datatypes through the use of interfaces. Interfaces can […]