码迷,mamicode.com
首页 > Web开发 > 详细

PHP 工厂模式浅析

时间:2018-04-27 19:44:45      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:echo   nts   cep   tor   ace   show   ret   sts   name   


//抽象出一个人的接口
interface Person{
public function showInfo();
}
//继承于人的学生类
class Student implements Person{
public function showInfo()
{
// TODO: Implement showInfo() method.
echo "我是一个学生";
}
}
//继承于人的教师类
class Teacher implements Person{
public function showInfo()
{
// TODO: Implement showInfo() method.
echo "我是一个老师";
}
}
//人类工厂
class PersonFactory{
public static function factory($person_type){
    //传进来的人的类型,首字母大写
$class_name = ucfirst($person_type);
if (class_exists($class_name)){
return new $class_name;
}else{
throw new Exception("类:".$class_name."不存在");
}
}
}
//学生类的实例化
$student = PersonFactory::factory(‘student‘);
$student->showInfo();

PHP 工厂模式浅析

标签:echo   nts   cep   tor   ace   show   ret   sts   name   

原文地址:https://www.cnblogs.com/kgtest/p/8963699.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!