In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
本篇内容主要讲解"php工厂模式怎么实现",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"php工厂模式怎么实现"吧!
说明
1、工厂模式解决的是如何不通过new建立实例对象。
2、有一些方法可以创建目标,而不是new。
如果想改变创建的对象类型,只需要改变工厂,使用这个工厂的所有代码会自动改变。
3、通常与接口一起使用,应用程序不需要知道这些实例化类的具体细节。
只要知道工厂返回的是支持某个接口类别的类就可以轻松使用。
实例
/** * 抽象出一个人的接口 * Interface Person */interface Person{ public function showInfo();} /** * 一个继承于抽象人接口的学生类 * Class Student */class Student implements Person{ public function showInfo() { echo "这是一个学生 \n"; }} /** * 一个继承于抽象人接口的老师类 * Class Teacher */class Teacher implements Person{ public function showInfo() { echo "这是一个老师 \n"; }} /** * 人类工厂 * Class PersonFactory */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 不存在",1); } }} // 需要一个学生$student = PersonFactory::factory('student');echo $student->showInfo(); // 需要一个老师的时候$teacher = PersonFactory::factory('teacher');echo $teacher->showInfo();到此,相信大家对"php工厂模式怎么实现"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.