Thursday, 9 February 2012

Factory Pattern PHP

<?php

/*
The Factory pattern allows for the instantiation of objects at runtime. It is called a Factory Pattern since it is responsible for "manufacturing" an object. A Parameterized Factory receives the name of the class to instantiate as argument.

*/


interface IUser
{
  function getName();
}

class User implements IUser
{
  public function __construct($id) { }

  public function getName()
  {
    return "Jack";
  }
}

class UserFactory
{
  public static function Create($id)
  {
    return new User($id);
  }
}

$uo = UserFactory::Create(1);
echo($uo->getName()."\n");
?>

No comments:

Post a Comment