{"id":540,"title":"php\u901a\u8fc7\u53cd\u5c04\u6765\u5b9e\u73b0\u4f9d\u8d56\u6ce8\u5165\u7b80\u5355\u5217\u5b50","good":0,"bad":0,"hit":1572,"created_at":"2021-08-27 23:13:02","content":"

\u901a\u8fc7PHP\u7c7b\u7684\u53cd\u5c04\u6765\u5b9e\u73b0\u4f9d\u8d56\u6ce8\u5165<\/p>

PHP\u5177\u6709\u5b8c\u6574\u7684\u53cd\u5c04 API\uff0c\u63d0\u4f9b\u4e86\u5bf9\u7c7b\u3001\u63a5\u53e3\u3001\u51fd\u6570\u3001\u65b9\u6cd5\u548c\u6269\u5c55\u8fdb\u884c\u9006\u5411\u5de5\u7a0b\u7684\u80fd\u529b\u3002\u901a\u8fc7\u7c7b\u7684\u53cd\u5c04\u63d0\u4f9b\u7684\u80fd\u529b\u6211\u4eec\u80fd\u591f\u77e5\u9053\u7c7b\u662f\u5982\u4f55\u88ab\u5b9a\u4e49\u7684\uff0c\u5b83\u6709\u4ec0\u4e48\u5c5e\u6027\u3001\u4ec0\u4e48\u65b9\u6cd5\u3001\u65b9\u6cd5\u90fd\u6709\u54ea\u4e9b\u53c2\u6570\uff0c\u7c7b\u6587\u4ef6\u7684\u8def\u5f84\u662f\u4ec0\u4e48\u7b49\u5f88\u91cd\u8981\u7684\u4fe1\u606f\u3002<\/span>\u4e5f\u6b63\u5f0f\u56e0\u4e3a\u7c7b\u7684\u53cd\u5c04\u5f88\u591aPHP\u6846\u67b6\u624d\u80fd\u5b9e\u73b0\u4f9d\u8d56\u6ce8\u5165\u81ea\u52a8\u89e3\u51b3\u7c7b\u4e0e\u7c7b\u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u8fd9\u7ed9\u6211\u4eec\u5e73\u65f6\u7684\u5f00\u53d1\u5e26\u6765\u4e86\u5f88\u5927\u7684\u65b9\u4fbf\u3002 \u672c\u6587\u4e3b\u8981\u662f\u8bb2\u89e3\u5982\u4f55\u5229\u7528\u7c7b\u7684\u53cd\u5c04\u6765\u5b9e\u73b0\u4f9d\u8d56\u6ce8\u5165(Dependency Injection)\uff0c\u5e76\u4e0d\u4f1a\u53bb\u9010\u6761\u8bb2\u8ff0PHP Reflection\u91cc\u7684\u6bcf\u4e00\u4e2aAPI\uff0c\u8be6\u7ec6\u7684API\u53c2\u8003\u4fe1\u606f\u8bf7\u67e5\u9605\u5b98\u65b9\u6587\u6863<\/a><\/p>

\u518d\u6b21\u58f0\u660e\u8fd9\u91cc\u5b9e\u73b0\u7684\u4f9d\u8d56\u6ce8\u5165\u975e\u5e38\u7b80\u5355\uff0c\u5e76\u4e0d\u80fd\u5e94\u7528\u5230\u5b9e\u9645\u5f00\u53d1\u4e2d\u53bb\uff0c\u53ef\u4ee5\u53c2\u8003\u53e6\u4e00\u7bc7\u6587\u7ae0\u6765\u4e86\u89e3laravel\u6846\u67b6\u7684\u670d\u52a1\u5bb9\u5668(IocContainer)<\/a>, \u5728\u90a3\u91cc\u4f1a\u4ecb\u7ecdLaravel\u7684\u670d\u52a1\u5bb9\u5668\u662f\u5982\u4f55\u5b9e\u73b0\u4f9d\u8d56\u6ce8\u5165\u7684\u3002<\/span><\/p>

\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\uff0c\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u4f8b\u5b50\u6765\u770b\u7c7b\u7684\u53cd\u5c04\uff0c\u4ee5\u53ca\u5982\u4f55\u5b9e\u73b0\u4f9d\u8d56\u6ce8\u5165\u3002\u4e0b\u9762\u8fd9\u4e2a\u7c7b\u4ee3\u8868\u4e86\u5750\u6807\u7cfb\u91cc\u7684\u4e00\u4e2a\u70b9\uff0c\u6709\u4e24\u4e2a\u5c5e\u6027\u6a2a\u5750\u6807x\u548c\u7eb5\u5750\u6807y\u3002<\/p>

\/**\n * Class Point\n *\/\n class Point{\n         public $x;\n         public $y;\n         \/**\n     * Point constructor.\n     * @param int $x  horizontal value of point's coordinate\n     * @param int $y  vertical value of point's coordinate\n     *\/\n    public function __construct($x = 0, $y = 0)\n    {\n            $this->x = $x;        \n            $this->y = $y;\n    }\n}<\/pre>

\u63a5\u4e0b\u6765\u8fd9\u4e2a\u7c7b\u4ee3\u8868\u5706\u5f62\uff0c\u53ef\u4ee5\u770b\u5230\u5728\u5b83\u7684\u6784\u9020\u51fd\u6570\u91cc\u6709\u4e00\u4e2a\u53c2\u6570\u662fPoint<\/code>\u7c7b\u7684\uff0c\u5373Circle<\/code>\u7c7b\u662f\u4f9d\u8d56\u4e0ePoint<\/code>\u7c7b\u7684\u3002<\/p>

class Circle{    \n    \/**\n     * @var int\n     *\/\n    public $radius;\/\/\u534a\u5f84\n\n    \/**\n     * @var Point\n     *\/\n    public $center;\/\/\u5706\u5fc3\u70b9\n\n    const PI = 3.14;    \n    public function __construct(Point $point, $radius = 1){\n        $this->center = $point;\n        $this->radius = $radius;\n    }    \n    \/\/\u6253\u5370\u5706\u70b9\u7684\u5750\u6807\n    public function printCenter(){\n        printf('center coordinate is (%d, %d)', $this->center->x, $this->center->y);\n    }\n    \/\/\u8ba1\u7b97\u5706\u5f62\u7684\u9762\u79ef\n    public function area(){\n        return 3.14 * pow($this->radius, 2);\n    }\n}<\/pre>

ReflectionClass#<\/a><\/h2>

\u4e0b\u9762\u6211\u4eec\u901a\u8fc7\u53cd\u5c04\u6765\u5bf9Circle<\/code>\u8fd9\u4e2a\u7c7b\u8fdb\u884c\u53cd\u5411\u5de5\u7a0b\u3002\u628aCircle<\/code>\u7c7b\u7684\u540d\u5b57\u4f20\u9012\u7ed9reflectionClass<\/code>\u6765\u5b9e\u4f8b\u5316\u4e00\u4e2aReflectionClass<\/code>\u7c7b\u7684\u5bf9\u8c61\u3002<\/p>

$reflectionClass = new reflectionClass(Circle::class);\/\/\u8fd4\u56de\u503c\u5982\u4e0bobject(ReflectionClass)#1 (1) {\n  ["name"]=>  string(6) "Circle"}<\/pre>

\u53cd\u5c04\u51fa\u7c7b\u7684\u5e38\u91cf#<\/a><\/h2>
$reflectionClass->getConstants();<\/pre>

\u8fd4\u56de\u4e00\u4e2a\u7531\u5e38\u91cf\u540d\u79f0\u548c\u503c\u6784\u6210\u7684\u5173\u8054\u6570\u7ec4<\/p>

array(1) {\n  ["PI"]=>  float(3.14)\n}<\/pre>

\u901a\u8fc7\u53cd\u5c04\u83b7\u53d6\u5c5e\u6027#<\/a><\/h2>
$reflectionClass->getProperties();<\/pre>

\u8fd4\u56de\u4e00\u4e2a\u7531ReflectionProperty\u5bf9\u8c61\u6784\u6210\u7684\u6570\u7ec4<\/p>

array(2) {\n  [0]=>  object(ReflectionProperty)#2 (2) {\n    ["name"]=>    string(6) "radius"\n    ["class"]=>    string(6) "Circle"\n  }\n  [1]=>  object(ReflectionProperty)#3 (2) {\n    ["name"]=>    string(6) "center"\n    ["class"]=>    string(6) "Circle"\n  }\n}<\/pre>

\u53cd\u5c04\u51fa\u7c7b\u4e2d\u5b9a\u4e49\u7684\u65b9\u6cd5#<\/a><\/h2>
$reflectionClass->getMethods();<\/pre>

\u8fd4\u56deReflectionMethod\u5bf9\u8c61\u6784\u6210\u7684\u6570\u7ec4<\/p>

array(3) {\n  [0]=>  object(ReflectionMethod)#2 (2) {\n    ["name"]=>    string(11) "__construct"\n    ["class"]=>    string(6) "Circle"\n  }\n  [1]=>  object(ReflectionMethod)#3 (2) {\n    ["name"]=>    string(11) "printCenter"\n    ["class"]=>    string(6) "Circle"\n  }\n  [2]=>  object(ReflectionMethod)#4 (2) {\n    ["name"]=>    string(4) "area"\n    ["class"]=>    string(6) "Circle"\n  }\n}<\/pre>

\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7getConstructor()<\/code>\u6765\u5355\u72ec\u83b7\u53d6\u7c7b\u7684\u6784\u9020\u65b9\u6cd5\uff0c\u5176\u8fd4\u56de\u503c\u4e3a\u4e00\u4e2aReflectionMethod<\/code>\u5bf9\u8c61\u3002<\/p>

$constructor = $reflectionClass->getConstructor();<\/pre>

\u53cd\u5c04\u51fa\u65b9\u6cd5\u7684\u53c2\u6570#<\/a><\/h2>
$parameters = $constructor->getParameters();<\/pre>

\u5176\u8fd4\u56de\u503c\u4e3aReflectionParameter\u5bf9\u8c61\u6784\u6210\u7684\u6570\u7ec4\u3002<\/span><\/p>

array(2) {\n  [0]=>  object(ReflectionParameter)#3 (1) {\n    ["name"]=>    string(5) "point"\n  }\n  [1]=>  object(ReflectionParameter)#4 (1) {\n    ["name"]=>    string(6) "radius"\n  }\n}<\/pre>

ReflectionParameter::getClass() \u83b7\u53d6\u53c2\u6570\u7684\u7c7b\u578b\u63d0\u793a\u7c7b\uff0c\u7c7b\u578b\u4e3aReflectionClass\u5bf9\u8c61\u3002<\/span><\/p>

var_dump($parameters [0]->getClass());\nvar_dump($parameters [0]->getClass()->name);<\/pre>
object(ReflectionClass)#10 (1) {\n  ["name"]=>  string(5) "Point"}string(5) "Point"<\/pre>

\u4f9d\u8d56\u6ce8\u5165#<\/a><\/h2>

\u597d\u4e86\u63a5\u4e0b\u6765\u6211\u4eec\u7f16\u5199\u4e00\u4e2a\u540d\u4e3amake<\/code>\u7684\u51fd\u6570\uff0c\u4f20\u9012\u7c7b\u540d\u79f0\u7ed9make<\/code>\u51fd\u6570\u8fd4\u56de\u7c7b\u7684\u5bf9\u8c61\uff0c\u5728make<\/code>\u91cc\u5b83\u4f1a\u5e2e\u6211\u4eec\u6ce8\u5165\u7c7b\u7684\u4f9d\u8d56\uff0c\u5373\u5728\u672c\u4f8b\u4e2d\u5e2e\u6211\u4eec\u6ce8\u5165Point<\/code>\u5bf9\u8c61\u7ed9Circle<\/code>\u7c7b\u7684\u6784\u9020\u65b9\u6cd5\u3002<\/p>

\/\/\u6784\u5efa\u7c7b\u7684\u5bf9\u8c61\nfunction make($className){\n    $reflectionClass = new ReflectionClass($className);\n    $constructor = $reflectionClass->getConstructor();\n    $parameters  = $constructor->getParameters();\n    $dependencies = getDependencies($parameters);    \n    return $reflectionClass->newInstanceArgs($dependencies);\n}\n\/\/\u4f9d\u8d56\u89e3\u6790\nfunction getDependencies($parameters){\n    $dependencies = [];\n    foreach($parameters as $parameter) {\n        $dependency = $parameter->getClass();\n        if (is_null($dependency)) {\n        \/\/\u7c7b\u540d\u4e3a\u7a7a\u65f6\uff0c \u5224\u65ad\u662f\u5426\u6709\u9ed8\u8ba4\u503c\n            if($parameter->isDefaultValueAvailable()) {\n                $dependencies[] = $parameter->getDefaultValue();\n            } else {\n                    \/\/\u4e0d\u662f\u53ef\u9009\u53c2\u6570\u7684\u4e3a\u4e86\u7b80\u5355\u76f4\u63a5\u8d4b\u503c\u4e3a\u5b57\u7b26\u4e320\n                \/\/\u9488\u5bf9\u6784\u9020\u65b9\u6cd5\u7684\u5fc5\u987b\u53c2\u6570\u8fd9\u4e2a\u60c5\u51b5\n                \/\/laravel\u662f\u901a\u8fc7service provider\u6ce8\u518cclosure\u5230IocContainer,\n                \/\/\u5728closure\u91cc\u53ef\u4ee5\u901a\u8fc7return new Class($param1, $param2)\u6765\u8fd4\u56de\u7c7b\u7684\u5b9e\u4f8b\n                \/\/\u7136\u540e\u5728make\u65f6\u56de\u8c03\u8fd9\u4e2aclosure\u5373\u53ef\u89e3\u6790\u51fa\u5bf9\u8c61\n                \/\/\u5177\u4f53\u7ec6\u8282\u4f1a\u5728\u53e6\u4e00\u7bc7\u6587\u7ae0\u91cc\u9762\u63cf\u8ff0\n                $dependencies[] = '0';\n            }\n        } else {\n        \/\/\u9012\u5f52\u89e3\u6790\u51fa\u4f9d\u8d56\u7c7b\u7684\u5bf9\u8c61\n            $dependencies[] = make($parameter->getClass()->name);\n        }\n    }    return $dependencies;\n}<\/pre>

ReflectionParameter::isDefaultValueAvailable<\/span> - \u68c0\u67e5\u662f\u5426\u6709\u9ed8\u8ba4\u503c\u3002.<\/p><\/blockquote>

ReflectionClass::newInstanceArgs()<\/span> \u521b\u5efa\u4e00\u4e2a\u7c7b\u7684\u65b0\u5b9e\u4f8b\uff0c\u7ed9\u51fa\u7684\u53c2\u6570\u5c06\u4f20\u9012\u5230\u7c7b\u7684\u6784\u9020\u51fd\u6570\u3002<\/p>

ReflectionClass::newInstanceArgs( [ array $args ] ) \u8fd9\u4e2a\u53c2\u6570\u4ee5\u6570\u7ec4\u5f62\u5f0f\u4f20\u9012\u5230\u7c7b\u7684\u6784\u9020\u51fd\u6570\u3002<\/p>

\u8fd4\u56de\u7c7b\u7684\u65b0\u5b9e\u4f8b\u3002<\/p><\/blockquote>

\u5b9a\u4e49\u597dmake<\/code>\u65b9\u6cd5\u540e\u6211\u4eec\u901a\u8fc7\u5b83\u6765\u5e2e\u6211\u4eec\u5b9e\u4f8b\u5316Circle\u7c7b\u7684\u5bf9\u8c61:<\/p>

$circle = make('Circle');$area = $circle->area();\n\/*var_dump($circle, $area);\nobject(Circle)#6 (2) {\n  ["radius"]=>\n  int(1)\n  ["center"]=>\n  object(Point)#11 (2) {\n    ["x"]=>\n    int(0)\n    ["y"]=>\n    int(0)\n  }\n}\nfloat(3.14)*\/<\/pre>

\u901a\u8fc7\u4e0a\u9762\u8fd9\u4e2a\u5b9e\u4f8b\u6211\u7b80\u5355\u63cf\u8ff0\u4e86\u4e00\u4e0b\u5982\u4f55\u5229\u7528PHP\u7c7b\u7684\u53cd\u5c04\u6765\u5b9e\u73b0\u4f9d\u8d56\u6ce8\u5165\uff0cLaravel\u7684\u4f9d\u8d56\u6ce8\u5165\u4e5f\u662f\u901a\u8fc7\u8fd9\u4e2a\u601d\u8def\u6765\u5b9e\u73b0\u7684\uff0c\u53ea\u4e0d\u8fc7\u8bbe\u8ba1\u7684\u66f4\u7cbe\u5bc6\u5927\u91cf\u5730\u5229\u7528\u4e86\u95ed\u5305\u56de\u8c03\u6765\u5e94\u5bf9\u5404\u79cd\u590d\u6742\u7684\u4f9d\u8d56\u6ce8\u5165\uff0c\u8be6\u60c5\u53ef\u4ee5\u53c2\u8003\u53e6\u4e00\u7bc7\u4ecb\u7ecdLaravel\u670d\u52a1\u5bb9\u5668<\/a>\u7684\u6587\u7ae0\u3002<\/p>

<\/p>

https:\/\/www.cnblogs.com\/caibaotimes\/p\/14644921.html<\/a><\/p>

<\/p>"}