Archive for the ‘design patterns’ Category

Class flags with bitwise operators

There is a group of  PHP features that most of developers don’t usually use: bitwise operators. These operators are widely used in the C language to make low-level applications (device drivers, protocols etc..). They are very useful when used in OOP developing as a flags for options in functions, classes and general settings. Brief overview [...]

Singleton is bad !!

An interesting suggestion emerged from some speakers (1 and 2) of the last PHP UK conf is: do not use the Singleton design pattern !! At the beginning it made me a little astonished as it’s one of the most widely used design patterns, in addition ZF and lots of other projects use it. However, [...]

Design Pattern: Decorator

Decorator is similar to Proxy. Basically, decorator is an extension (or wrapper) of the original class (component) and adds features. A decorator contains a pointer to the component class (initialized by the constructor), and the methods of the component overridden. Example: class ImgComponent {private $src;private $alt;public function __construct($name, $src) { /* assign to properties*/ };public [...]

Design Pattern: Adapter

The Adapter design pattern is basically a wrapper, that is a class which provides methods to access to another class. It’s commonly used to normalize incompatibile interfaces. It’s similar to Proxy pattern, but it’s a little different. Proxy includes an object and controls accesses to it; Adapter provides an interface to the object, usually by [...]

Design Pattern: Proxy

It provides a filtered controlled access to an object, without exposing it directly. How to implement in PHP5: Declare a class: constructor: instantiates the object (only the first time) and keeps the instance into a private properties – implement the methods that provide a controlled access to the internal objector- use the magic method __call($name, [...]

Design Pattern: Factory

Factory is a creational design pattern which handles object creation. A factory class provides the methods to create object (whit a superclass in common), but let subclasses to decide which class will be instantiated.Basically, it contains a static method (“connect” in the example below) that accept one parameter and makes a switch on it. Depending [...]

Design Pattern: Value

Consider a class Val which contains a value, and a method add() which add another value with the same type. class Val { private $value; public function __construct($value=0) {$this->value = $value; } public function get() { return $this->value; } public function add($newVal) { //(1) WRONG: //$this->addV += $value; //WRONG !! // (2) RIGHT !! return [...]

Design Pattern Singleton

Singleton is a creational pattern which ensures a class has only one instance, and provide a global pointer to access it. The most common example is a database connection. The idea: the class doesn’t provide a constructor, but a method to return an instance. That instance is assigned to a static properties and is instantiated [...]