根据业务需求工厂模式动态生成类的实例学习

Original 2018-11-19 11:21:28 149
abstract:
 work(); // } // public function drive() // { // $auto = new Auto(); // return '汽车'.$auto->run(); // } // } // $student = new Student; // echo $student->study(); // echo '
'; // echo $student->drive(); // echo '


'; //解决方法测试 //1.创建工厂类: Factory class Factory { public static function create($className) { switch (strtolower($className)) { case 'computer': return new Computer(); break; case 'auto': return new Auto(); break; } } } class Student1 { public function study() { $computer = Factory::create('Computer'); //Factory::create(); 知识点 Factory方法 return '计算机'.$computer-> work(); } public function drive() { $auto = Factory::create('Auto');; return '汽车'.$auto->run(); } } $student1 = new Student1; echo $student1->study(); echo '
'; echo $student1->drive();


Release Notes

Popular Entries