Relevant Patterns for Construction of a CMS
The singleton appears repeatedly, especially for the handler objects described later. In theory, handlers could be implemented as class methods, but in PHP5 a class is not itself a first class object and it is more effective to use singleton objects. Other objects are naturally singletons, such as the session object, since PHP5 handles only a single request at a time.
Factories are used to create objects when the circumstances will alter what class is required. For example, a factory is used to create the session object which will be of a different class depending on whether the session belongs to an administrator or an ordinary user.
Observer (sometimes called subject-observer or publish-subscribe) pattern is implemented more as an architecture to handle plug ins than as PHP5 classes. This is because the actual process of adding a plug in is an installation that affects the database more than the object structure.
The Command pattern is also used to handle plug ins, since the external interface for plug ins must be completely generalized. A Factory is used to create the correct plug in object to handle a particular request.
The Bridge and Memento patterns can also be used in the construction of plug ins.
Model, View, Controller
It has long been agreed that it is good to make a separation between the model and views of the model. The model is understood as the set of objects that emulates the problem being solved by a computer application...Views may well change in different ways and at different times from the model...The controller manages the situation by handling input from the user and organizing the required objects, then supplying them to the appropriate view...The role of the controller then includes marshalling data and somehow transferring it into the template system.
Expert insight and practical guidance to creating an efficient, flexible, and robust framework for a PHP5-based content management system
- By: Martin Brampton
- Publisher: Packt Publishing
- Pub. Date: June 06, 2008
- ISBN 978-1-84719-357-5
PHP will not fail if variables are uninitialized, it will assume that they are null and issue a notice to tell you about it.
Declarations are powerful, and it pays to maximize their power. Classes can be declared as abstract when they are not intended to be used on their own to create objects, but used only to build subclasses. Methods can be declared as public, private, or protected and the most suitable options should always be chosen. Variables inside an object should be declared wherever possible, and like methods, their visibility should be stated. In line with the previous comments, it is a good idea to initialize every declared variable in a class with a reasonably safe value.
Efficiency is achieved by good design and avoiding writing unnecessary code. The fastest code is the code that has been factored out of the design and no longer exists!
Bearing this in mind, what I advocate is an approach to object orientation where the classes and objects come naturally from the problem being solved. Arbitrary rules for class building are best avoided in favor of letting the problem show through the code. And although patterns can be immensely valuable, they should also be treated with some caution.
These are notes I made after reading this book. See more book notes