How to implement a decorator in PHP?

I would suggest that you also create a unified interface (or even an abstract base class) for the decorators and the objects you want decorated. To continue the above example provided you could have something like: interface IDecoratedText { public function __toString(); } Then of course modify both Text and LeetText to implement the interface. … Read more

What is Protocol Oriented Programming in Swift? What added value does it bring?

Preface: POP and OOP are not mutually exclusive. They’re design paradigms that are greatly related. The primary aspect of POP over OOP is that is prefers composition over inheritance. There are several benefits to this. In large inheritance hierarchies, the ancestor classes tend to contain most of the (generalized) functionality, with the leaf subclasses making … Read more

PHP equivalent of friend or internal

PHP doesn’t support any friend-like declarations. It’s possible to simulate this using the PHP5 __get and __set methods and inspecting a backtrace for only the allowed friend classes, although the code to do it is kind of clumsy. There’s some sample code and discussion on the topic on PHP’s site: class HasFriends { private $__friends … Read more

Should I use public or private variables?

private data members are generally considered good because they provide encapsulation. Providing getters and setters for them breaks that encapsulation, but it’s still better than public data members because there’s only once access point to that data. You’ll notice this during debugging. If it’s private, you know you can only modify the variable inside the … Read more

OO Javascript constructor pattern: neo-classical vs prototypal

This looks like the non-singleton version of the module pattern, whereby private variables can be simulated by taking advantage of JavaScript’s “closures”. I like it (kinda…). But I don’t really see the advantage in private variables done in this way, especially when it means that any new methods added (after initialisation) do not have access … Read more

What are the advantages that prototype based OO has over class based OO?

The advantage of prototypal inheritance is that it potentially allows fancy metaprogramming in a straightforward way because the prototype chain is easily manipulated. This is a rather academic advantage because metaprogramming is the wrong answer 99% of the time. As an example, you could have a Javascript Key-Value Observer style data manipulation layer with a … Read more