Multitons in PHP

In my last blog I covered the singleton design pattern (DP), showing how and when to use it. In this article I am going to cover a very similar design pattern called the multiton.

A multiton is almost exactly the same as a singleton, the one slight difference is a multiton can store multiple instances of itself. Contrary to a singleton, which can only store one instance of itself.

Continue reading

Singletons in PHP

If you’ve heard of design patterns you’ll have probably heard of the singleton pattern. It’s debatably the easiest to start with and get you on the road to becoming the Walter White of design patterns. Below I will explain what a singleton is, when you should use a singleton, rules that must be followed, drawbacks and I will provide sample code. Lets get started.

What is a singleton

As the name sort of implies, a singleton is a class that can only be initialised once per request to an application. E.g once per HTTP request on a website.

Continue reading

Design Patterns

Recently I’ve started posting a small series of OO related articles, they cover the APIE concepts and the SOLID principles, if you haven’t read these yet, I strongly suggest you do. Once you’ve got an understanding of how to write maintainable and extensible code, the next step is learn how to solve common programming problems using design patterns.

Design patterns have been around for decades, they were first defined properly by the Gang of Four (GoF). Since being defined they haven’t changed that much, in fact many patterns haven’t changed at all. Each pattern is placed into a group, based on what it attempts to achieve. These group are, creational, structural, behavioural and concurrent.

There are also architectural patterns, these are similar to design patterns, but they have a broader scope, taking the entire system into account, where a design pattern is more specific to an area of code.

Continue reading

SOLID Development

Since writing this article, I have rewritten it in much more detail.

In my last article I posted about “What is APIE?“, if you haven’t read this yet and don’t know what APIE is, I recommend reading that article first.

Just like APIE states the 4 basic concepts of OOP, SOLID defines 5 OOP principles which you should always follow when writing code. The letters stand for Single responsibility, Open-closed, Liskov substitution, Interface segregation and Dependency inversion. As much as all of these principles help in achieving a maintainable code base, the Open-closed and Interface segregation principles aren’t as useful as the others in my opinion as they’re harder to implement effectively and sometimes don’t give much benefit. Regardless I will explain what you need to know about each of these.

Continue reading