public function invokeClass(string $class, array $vars = []) { try { //根據反射機制獲取 類的內部信息(多少個私有屬性,多少個共有屬性等等) $reflect = new ReflectionClass($class); } catch (ReflectionException $e) { throw new ClassNotFoundException('class not exists: ' . $class, $class, $e); } if ($reflect->hasMethod('__make')) { $method = $reflect->getMethod('__make'); if ($method->isPublic() && $method->isStatic()) { $args = $this->bindParams($method, $vars); return $method->invokeArgs(null, $args); } } //獲取構造函數 $constructor = $reflect->getConstructor(); //分析構造函數的參數,並獲取具體的參數 $args = $constructor ? $this->bindParams($constructor, $vars) : []; //帶參數實例化對應的類 $object = $reflect->newInstanceArgs($args); $this->invokeAfter($class, $object); return $object; }
TP6 依賴注入
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.