【问题标题】:ReflectionClass keeps returning className does not existReflectionClass 不断返回 className 不存在
【发布时间】:2020-09-09 02:23:15
【问题描述】:

我要做的是使用scandir() 扫描目录并获取那里的所有文件,然后使用ReflectionClass 读取或访问该类。

我能够获取目录中的所有文件并将其分配给一个数组,但无法使用\ReflectionClass 读取或访问methodsmethods,因为它不断返回

类 App\Controllers\AtController 不存在

这是我目前的代码

// The directory to scan and assign it to a variable
$classControllers = scandir($cur_dir . '/app/Controllers');

// Change to the ReflectionClass to access
chdir($cur_dir . '/app/Controllers/');

// Dump the $classControllers to see if it truly scan the directory
var_dump($classControllers);

$control = '';

// Loop over the $classControllers
foreach($classControllers as $classController){
    if($classController != "." && $classController != "..")
    {
        $classController = str_ireplace(".php", "", $classController);
        echo $classController . PHP_EOL;

        // Use ReflectionClass to read the class name, methods and properties of each class.
        $controllers = new \ReflectionClass("App\\Controllers\\$classController")#App\Controllers is the namespace of the files in the directory;

        $controller = $controllers->getName();
        $control = substr($controller, 12);
        $control = ucfirst($control);
        $routeScript .= "\t$control" . "," . PHP_EOL; 
    }
}

注意:new \ReflectionClass("App\\Controllers\\$classController"); #App\Controllers is the namespace of the files in the directory

【问题讨论】:

  • 你是否使用自动加载来加载类?
  • 是的,我使用自动加载来加载它
  • 你是如何设置自动加载的?
  • 尝试在$classController = str_ireplace(".php", "", $classController);行前加include_once $classController;看看。
  • @KoalaYeung 我使用 composer.json 设置了自动加载

标签: php reflection php-7 php-7.2


【解决方案1】:

根据我的经验,这个问题通常来自两个来源:

  1. 自动加载设置不正确;和
  2. 类文件中类名或命名空间中的错字。

如果是第一个问题,在文件中添加一个简单的行即可解决问题:

<?php

$cur_dir = __DIR__;

// The directory to scan and assign it to a variable
$classControllers = scandir($cur_dir . '/app/Controllers');

// Change to the ReflectionClass to access
chdir($cur_dir . '/app/Controllers/');

// Dump the $classControllers to see if it truly scan the directory
var_dump($classControllers);

$control = '';
$routeScript = '';

// Loop over the $classControllers
foreach($classControllers as $classController){
    if($classController != "." && $classController != "..")
    {
        include_once $classController; // <-- this line
        $classController = str_ireplace(".php", "", $classController);
        echo $classController . PHP_EOL;

        // Use ReflectionClass to read the class name, methods and properties of each class.
        $controllers = new \ReflectionClass("App\\Controllers\\$classController"); #App\Controllers is the namespace of the files in the directory;

        $controller = $controllers->getName();
        $control = substr($controller, 12);
        $control = ucfirst($control);
        $routeScript .= "\t$control" . "," . PHP_EOL;
    }
}

请同时检查第二个问题。命名空间和类名都区分大小写。两者都很容易出现拼写错误:

<?php

namespace App\Controllers;

class AtController {
   // ...
}

【讨论】:

  • 感谢@Koala Yeung 拯救了我的一天。问题是第一种情况(自动加载设置不正确)。我从不将 app 目录包含在 composer.json 文件中
猜你喜欢
  • 1970-01-01
  • 2017-03-06
  • 2017-09-05
  • 1970-01-01
  • 1970-01-01
  • 2019-11-05
  • 2013-06-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多