The 5 Most Common Design Patterns in PHP Applications

Igor Vorobiov
7 min readFeb 27, 2018
Photo by Neil Thomas on Unsplash

If you think that the number one pattern is Singleton then you are fired! The Singleton pattern is already deprecated, and not wanted and even hated.

Let’s take a look at the 5 most commonly used designed patterns in the PHP world these days.

Factory

You should use factories when you want to build an object. That’s right — build and not create. You don’t want to have a factory just to create a new object. When you build the object you first create it and then initialize it. Usually, it requires to perform multiple steps and apply certain logic. With that, it makes sense to have all that in one place and re-use it whenever you need to have a new object built in the same way. Basically, that’s the point of the factory pattern.

It’s a good idea to have an interface for your factory and have your code depended on it and not on a concrete factory. With that, you can easily replace one factory with another whenever you need it.

interface FriendFactoryInterface {
public function create() : Friend
}

Next, we implement our factory interface with the following class:

class FriendFactory implements FriendFactoryInterface {    public function create() : Friend {…

--

--