<?php
/**
* Created by PhpStorm.
* User: huiting
* Date: 18-3-22
* Time: 上午9:18
*/
interface IUser{
function getName();
function getAge();
}
interface IOther{
function getName();
function oindex();
}
//
class User implements IUser{
public function __construct($id)
{
$this->user_id = $id;
}
public function getName()
{
// TODO: Implement getName() method.
return 'name'.$this->user_id;
}
public function getAge(){
return 'age'.$this->user_id;
}
}
class Other implements IOther{
public function __construct($id)
{
$this->id = $id;
}
public function getName(){
return 'other'.$this->id;
}
public function oindex(){
return 'ccv';
}
}
class Factory
{
// 返回学生姓名
public static function create($id){
return new User($id);
}
// 返回其他信息
public static function other($id){
return new Other($id);
}
// 根据不同的参数实例化不同的类
public static function toClass($id,$class='Other'){
return new $class($id);
}
public function index(){
return 'cc';
}
}
// 工厂模式1 就是一个工厂类 里面有其他生产类 在工厂类里面已经实例化过其他生产类 在生产过程中只需要针对工厂要对应的产品就可以啦
include 'Factory.php';
$c = Factory::toClass(4,'User');
echo $c->getAge().'<br>';
//echo $c->getName();
https://www.ibm.com/developerworks/cn/opensource/os-php-designptrns/