<?php // 设计模式之工厂模式 class Factory { static public function fac ($id) { switch ($id) { case 1: return new A();
case 2: return new B();
case 3: return new C();
default: return new D(); } } } /** * 创建一个接口 */ interFace FetchName { public function getname(); } class A implements FetchName { private $name = "A"; public function getname () { return $this->name; } } class B implements FetchName { private $name = "B"; public function getname () { return $this->name; } } class C implements FetchName { private $name = "C"; public function getname () { return $this->name; } } class D implements FetchName { private $name = "D"; public function getname () { return $this->name; } } $obj = Factory::fac(5); echo $obj->getname();