Zend PHP5 certification Exam study review 9: OOP Design&Theory

Here is the second topic regarding the OOP paradigm. Trust me, read more about Design Pattern, don’t just for pass the exam sake. The exam regard to the Design Pattern and general OOP theory is very basic. Anyway, following I listed some basic Design Pattern may tested in the exam. Also, I think you should look into the SPL(standard PHP Library) and PECL(PHP Extension Community Library) and PEAR(PHP Extension and Application Repository).
Design Pattern Theory

Design Patterns – Some time ago people realized that we were solving the same problems again and again, they then decided to sit down and come up with really good solutions to those problems, they’re called design patterns.

The advantages of design patterns are two fold: first they present a well defined solution to common problems, second they provide a common language for developers.

The Singleton Pattern

The singleton pattern is used to ensure that you only have one instance of a given class at a time.
This is useful in a myriad of circumstances, resource connections (database, file, external) most notably.

class singleton
private static $instance = null;
protected function __construct()
{ /* … */ }
public function getInstance()
if (self::$instance === null)
$class = __CLASS__;
self::$instance = new $class;
return self::$instance;
$connection = new singleton(); //Error
$connection = singleton::getInstance();

The Singleton is, probably, the simplest design pattern. Its goal is to provide access to a single resource that is never duplicated, but that is made available to any portion of an application that requests it without the need to keep track of its existence.

.database connection
make constructor private/protected

The Factory Pattern

The factory pattern is used to provide a common interface to a series of classes with identical functionality but different internals (think data storage, varying shipping systems, payment processing)

The “factory” provides an instance of the appropriate class.

The Factory pattern is used in scenarios where you have a generic class (the factory) that provides the facilities for creating instances of one or more separate “specialized” classes that handle the same task in different ways.
A good situation in which the Factory pattern provides an excellent solution is the management of multiple storage mechanisms for a given task.

An Factory Pattern example:
class Configuration {
const STORE_INI = 1;
const STORE_DB = 2;
const STORE_XML = 3;
public static function getStore($type = self::STORE_XML)
switch ($type) {
case self::STORE_INI:
return new Configuration_Ini();
case self::STORE_DB:
return new Configuration_DB();
case self::STORE_XML:
return new Configuration_XML();
throw new Exception(“Unknown Datastore Specified.”);

class Configuration_Ini {
// …

class Configuration_DB {
// …
class Configuration_XML {
// …
$config = Configuration::getStore(Configuration::STORE_XML);

class shippment
{ public static function getShippmentClass($method) {
switch ($method) {
case “Fedex”:
return new shipFedEx();
case “USPS”:
return new shipUSPS();
case “UPS”:
return new shipUPS();
throw new Exception(“Unknown shipment method requested”);
$shippingInterface = shippment::getShippmentClass(“Fedex”);

The Registry Pattern
By taking the Singleton pattern a little further, we can implement the Registry pattern. This allows us to use any object as a Singleton without it being written specifically that way.

An extension of the Singleton Pattern, that allows for differing functionality based on some input data.
Imagine using the Singleton pattern on a database, but occasionally requiring a connection to an alternate database, Registry provides this.

The Model-View-Controller Pattern MVC
.Complex pattern, the user initiates an action via the controller, which interfaces with the model, finally the view is called which takes care of dealing with the user interface.

.This clear distinction between various layers allows for modularity in code, entire levels of code can be swapped in and out.

.Many modern frameworks use this pattern, such as Symfony, CakePHP and Ruby on Rails.
Its goal is that of providing a methodology for separating the business logic (model) from the display logic (view) and the decisional controls (controller).
In a typical MVC setup, the user initiates an action (even a default one) by calling the Controller. This, in turn, interfaces with the Model, causing it to perform some sort of action and, therefore, changing its state. Finally, the View is called, thus causing the user interface to be refreshed to reflect the changes in the Model and the action requested of the Controller, and the cycle begins anew.

The ActiveRecord Pattern
Encapsulates a data source, allowing external code to concentrate on using the data while the active record pattern provides a consistent interface, hiding the work that goes into iterating over records, making changes etc.

Most commonly implemented with databases – One of the things people love about Ruby

This is used to encapsulate access to a data source so that the act of accessing its components—both for reading and for writing—is, in fact, hidden within the class that implements the pattern, allowing its callers to worry about using the data, as opposed to dealing with the database.

The Standard PHP Library SPL

.Standard PHP Library
.Extension available and compiled by default in PHP 5
.Improved in PHP 5.1

The Standard PHP Library (SPL) is a great addition to PHP 5. It provides a number of very useful facilities that expose some of PHP’s internal functionality and allow the “userland” developer to write objects that are capable of behaving like arrays, or that transparently implement certain iterative design patterns to PHP’s own core functionality, so that you, for example, use a foreach() construct to loop through an object as if it were an array, or even access its individual elements using the array operator [].

SPL works primarily by providing a number of interfaces that can be used to implement the functionality required to perform certain operations. By far, the largest number of patterns exposed by SPL are iterators.

Code Reuse
.We solve the same problems every day
.We solve the same problems as our neighbor every day
.We solve the same problems on every project
.Code Reuse allows us to cut development time while increasing code quality

.Array Access to objects The ArrayAccess interface

interface ArrayAccess {
function offsetSet($offset, $value);
function offsetGet($offset);
function offsetUnset($offset);
function offsetExists($offset);

.Simple Iteration The Iterator interface

interface Iterator {}
function current();
function next();
function rewind();
function key();
function valid();

.Seekable Iteration The SeekableIterator interface

interface SeekableIterator {
function current();
function next();
function rewind();
function key();
function valid();
function seek($index);

.Recursive Iteration The RecursiveIteratorIterator

.Filtered Iteration The FilterIterator


SPL contains a number of very useful code sets, take a look, they’re really useful.
Design patterns present common solutions to common problems, they also provide programmers with a common language when describing solutions.

18 Comments - Leave a comment
  1. Matthwe Bonner says:

    Cleaner singleton:

    static public function getInstance () {
    if (self::$_instance === NULL) self::$_instance = new self;
    return self::$_instance;

  2. Heena says:

    the following is an example of what design pattern?

    class MyClassBuilder{
    public function build(){
    return new MyClass();

    1>none of the above

  3. Heena says:

    hey i read the builder pattern but i dont think its builde bcoz bilder class should be abstract class … can u please expain why its builder pattern?

  4. Heena says:

    The MVC pattern in web development involves which of the following component?
    1> view
    2> controller
    3> validation
    4> model
    5> front controller

  5. Heena says:

    4 options to choose for mvc

  6. David Adam says:

    Hi, this is easy question. MVC stands for Model, View and Controller.

  7. David Adam says:

    If there are 4 options to choose for mvc, then the Front Controller is the one.

  8. David Adam says:

    “hey i read the builder pattern but i dont think its builde bcoz bilder class should be abstract class … can u please expain why its builder pattern?

    Hi, sorry. I just try my best to choose the answer.Actually, no one knows the right answer. I also rechecked the wikipedia, and think it is indeed quite not like the builder. So, maybe, the answer is 1>none of the above.

  9. Heena says:

    hi thanks a lot for your answer… hey in mock test questions about design pattern are very complicated… hows in real exam? i m in tension … i m going to give exam very soom … please tell me if u r there….

  10. David Adam says:

    Hi, IMO, the exam quzs on design patterns are actually very basic. They are just let you read some basic code and ask you which design pattern it uses. You will not require write codes to apply certain design pattern. However, I think the design pattern is very important in these days web development. Especially the MVC. I have a project right now using Zend Framework. Zend Framework is very good, especially if you have some knowledge regarding the design patterns. It is very easy to get start.
    Recommed book:

  11. J says:

    Thanks for all these notes. I have the Zend Cert Study Guide, but it does miss a few important areas that you have covered in your notes.

  12. David Adam says:

    J, thanks for your comment. I knew the 2nd version of the study guide is out. Not sure if it has some changes with the 1st edition.
    My notes is a combination of the study guide, mock test and several other books, plus some question I encountered in my previous projects. Hope my notes are useful for anyone who wish to pass the exam. :)

  13. Max says:

    Since there are many types of design patterns, would you please give us a list here? Thank you very much for your kind help!

  14. David Adam says:

    Hi Max. I don’t know the completed list. I have watched this tutoiral: http://www.vtc.com/products/Design-Patterns-tutorials.htm

    This may help?

  15. Zzzz says:

    I am really appreciate your effort for ZCE.

    My Questions: sure if you have a free time to answer me :)

    I want to ask if I passed the mock exam, is that mean I can passing the real exam?

    I mean is that real example more complicated than the mock exam ?

    Thank you

  16. bala says:


    I’m going to write PHP5.3 exam on Nov 2012. Please give me some more idea how to pass the exam PHP5.3 . I can say my stuff in PHP (6 out of 10).


Leave a Reply

Your email address will not be published. Required fields are marked *