PHP5之后提供了完整的反射API,添加了对类、接口、函数、方法和扩展进行反向工程的能力。此外,反射API提供了方法来取出函数、类和方法的文档注释。
Ioc/Di大家应该都不陌生,但是对小白来说呢听起来就挺高大上的,下面就用代码来实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| <?php
class Foo { public function getClassName() { return 'this is Foo'; } }
class Bar { public function getClassName() { return 'this is Bar'; } }
class Test { public function __construct(Foo $foo, Bar $bar) { var_dump($foo->getClassName()); var_dump($bar->getClassName()); }
public function index(Foo $foo) { var_dump($foo->getClassName()); } }
$reflect = new ReflectionClass(Test::class);
$constructor = $reflect->getConstructor(); if ($constructor) { $constructorParams = $constructor->getParameters(); $args = []; foreach ($constructorParams as $param) { if ($param->getClass()) { $args[] = $param->getClass()->newInstance(); } else { $args[] = $param->getName(); } } $class = $reflect->newInstanceArgs($args); } else { $class = $reflect->newInstance(); }
$reflectMethod = new ReflectionMethod($class, 'index');
if ($reflectMethod->isPublic()) { $args = []; $methodParams = $reflectMethod->getParameters(); foreach ($methodParams as $param) { if ($param->getClass()) { $args[] = $param->getClass()->newInstance(); } else { $args[] = $param->getName(); } } $reflectMethod->invokeArgs($class, $args); }
|
以上就简单的实现了依赖注入,下面我们接着封装一下。
Ioc.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| <?php
class Ioc { public static function getInstance($className) { $args = self::getMethodParams($className); return (new ReflectionClass($className))->newInstanceArgs($args); }
public static function make($className, $methodName, $params = []) {
$instance = self::getInstance($className);
$args = self::getMethodParams($className, $methodName);
return $instance->{$methodName}(...array_merge($args, $params)); }
protected static function getMethodParams($className, $methodsName = '__construct') {
$class = new ReflectionClass($className); $args = [];
if ($class->hasMethod($methodsName)) { $construct = $class->getMethod($methodsName);
$params = $construct->getParameters();
if (!$params) return $args;
foreach ($params as $param) {
if ($paramClass = $param->getClass()) {
$paramClassName = $paramClass->getName(); $methodArgs = self::getMethodParams($paramClassName);
$args[] = (new ReflectionClass($paramClass->getName()))->newInstanceArgs($methodArgs); } } } return $args; } }
|
以上代码实现了构造函数的依赖注入及方法的依赖注入,下面进行测试。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <?php class Bar { public function getClassName() { return 'this is Bar'; } }
class Test { public function __construct(Foo $foo, Bar $bar) { var_dump($foo->getClassName()); var_dump($bar->getClassName()); }
public function index(Foo $foo) { var_dump($foo->getClassName()); } } Ioc::getInstance(Test::class); Ioc::make(Test::class,'index');
|
以上呢,就简单的通过php的反射机制实现了依赖注入。
继基于swoole
的微服务框架出现,注解呢就开始慢慢出现在我们的视角里。据说php8也加入了注解支持:
1 2 3 4 5 6 7 8 9 10
| use \Support\Attributes\ListensTo;
class ProductSubscriber { <<ListensTo(ProductCreated::class)>> public function onProductCreated(ProductCreated $event) { }
<<ListensTo(ProductDeleted::class)>> public function onProductDeleted(ProductDeleted $event) { } }
|
就类似这样的,哈哈哈。
而我们现在的注解则是通过反射拿到注释去做到的解析。
接下来我们去用别人写好的组件去实现annotations
编写我们的composer.json:
1 2 3 4 5 6 7 8 9 10 11
| { "require": { "doctrine/annotations": "^1.8" }, "autoload": { "psr-4": { "app\\": "app/", "library\\": "library/" } } }
|
接下来要执行啥???这个你要是再不会,真的我劝你回家种地吧!!哈哈哈 闹着玩呢!
composer install
然后我们接下来去创建目录:
1 2 3 4 5 6 7 8 9 10 11 12
| - app // app目录 - Http - HomeController.php - library // 核心注解库 - annotation - Mapping - Controller.php - RequestMapping.php - Parser - vendor - composer.json - index.php // 测试文件
|

害 图片有点大。。。。。咋整。。。算了,就这样吧!!!
温馨提示:在phpstrom里面,安装插件PHP Annotation写代码会更友好啊!!
创建library\Mapping\Controller.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| <?php
namespace library\annotation\Mapping;
use Doctrine\Common\Annotations\Annotation\Attribute; use Doctrine\Common\Annotations\Annotation\Attributes; use Doctrine\Common\Annotations\Annotation\Required; use Doctrine\Common\Annotations\Annotation\Target;
final class Controller {
private $prefix = ''; public function __construct(array $value) { if (isset($value['value'])) $this->prefix = $value['value']; if (isset($value['prefix'])) $this->prefix = $value['prefix']; }
public function getPrefix(){ return $this->prefix; } }
|
@Annotation表示这是个注解类,让IDE提示更加友好!
@Target表示这个注解类只能被类使用!
@Required表示这个属性是必须填写的!
@Attributes表示这个注解类有多少个属性!
创建library\Mapping\RequestMapping.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| <?php
namespace library\annotation\Mapping;
use Doctrine\Common\Annotations\Annotation\Required; use Doctrine\Common\Annotations\Annotation\Target;
final class RequestMapping {
private $route; public function __construct(array $value) { if (isset($value['value'])) $this->route = $value['value']; if (isset($value['route'])) $this->route = $value['route']; }
public function getRoute(){ return $this->route; } }
|
这里的代码就不用再重复解释了吧!我偏不解释了!哈哈哈
创建app\Http\HomeController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| <?php
namespace app\Http;
use library\annotation\Mapping\Controller; use library\annotation\Mapping\RequestMapping;
class HomeController {
public function test(){ echo 111; } }
|
这里代码没啥解释的。。。
哈哈,下面来测试我们的结果!
index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| <?php
$loader = require './vendor/autoload.php';
$refClass = new \ReflectionClass('\app\Http\HomeController');
\Doctrine\Common\Annotations\AnnotationRegistry::registerLoader([$loader, 'loadClass']);
$reader = new \Doctrine\Common\Annotations\AnnotationReader();
$classAnnotations = $reader->getClassAnnotations($refClass);
foreach ($classAnnotations as $annotation){ if ($annotation instanceof \library\annotation\Mapping\Controller){ echo $routePrefix = $annotation->getPrefix().PHP_EOL; $refMethods = $refClass->getMethods(); }
foreach ($refMethods as $method){ $methodAnnotations = $reader->getMethodAnnotations($method); foreach ($methodAnnotations as $methodAnnotation){ if ($methodAnnotation instanceof \library\annotation\Mapping\RequestMapping){ echo $methodAnnotation->getRoute().PHP_EOL; } } } }
|
执行结果:
之前我们不是建立了个Parser的目录嘛,可以在里面创建对应的解析类,然后去解析,把它们封装一下子!!