Vinay Kumar

Observer pattern in PHP

Observer pattern is one among available design patterns in which an object(subject), holds a list of its dependents(observer) and notifies them automatically of any state changes. This pattern is useful there is one to many relationship.

To understand better, let’s design an error notifier which will have file logger and database logger as its dependents. Before going through the below example code if you are new to dependency injection then I recommend you to have a look at this post.

The output for the above code is

Logged to file with Test error. 
Logged to database with Test error.