123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- <?php
- /**
- * Created by PhpStorm
- * User:林志杰
- * Email:[email protected]
- * Motto:纵有疾风起,人生不言弃!
- * Time:2020/2/8 0:44
- */
- declare(strict_types=1);
- namespace app\common\lib;
- class ClassArr
- {
- public static function smsClassStat(): array
- {
- return [
- "ali" => "app\common\lib\sms\AliSms",
- "baidu" => "app\common\lib\sms\BaiduSms",
- "jd" => "app\common\lib\sms\JdSms",
- ];
- }
- public static function uploadClassStat(): array
- {
- return [
- 'text' => 'xxx',
- 'image' => 'xxx',
- ];
- }
- /**
- * @param string $type
- * @param array $class
- * @param array $params 实例化的参数
- * @param bool $needInstance 是否需要实例化
- * @return bool|mixed|object
- * @throws \ReflectionException
- */
- public static function initClass(string $type, array $class, array $params = [], bool $needInstance = false)
- {
- // 如果工厂模式调用的方法是静态的,那么我们这个地方返回类库AliSms
- // 如果不是静态的呢,我们就需要返回 对象
- if (!array_key_exists($type, $class)) {
- return false;
- }
- $className = $class[$type];
- // new \ReflectionClass('A') => 简历A的反射类
- // ->newInstanceArgs($args) => 返回A的实例化对象,$args是实例化时传入的参数
- return $needInstance === true ? (new \ReflectionClass('AliSms'))->newInstanceArgs($params) : $className;
- }
- }
|