【问题标题】:ReflectionClass changes name of class and causes fatal errorReflectionClass 更改类的名称并导致致命错误
【发布时间】:2015-02-16 12:14:44
【问题描述】:

在使用反射获取类实例时出现了一些严重的怪异现象。代码非常简单,我真的不知道哪里出了问题,似乎反射正在改变类名。所以:

$reflect = new ReflectionClass($class);
print_r($reflect);  
ReflectionClass Object
(
  [name] => Db_Table_Data_Binding
)

return $reflect->newInstance(); // fatal error class Db_Table_Binding Not Found 

即使我将其包装在 if 语句中以确认:

if($class === "Db_Table_Data_Binding")
{
   return $reflect->newInstance(); // fatal error class Db_Table_Binding 
}

真的看不到这里发生了什么......任何帮助都会很棒。

所以我为任何可能偶然发现此问题的人找出了我做错了什么..

class Db_Table_Data_Binding{
   protected $bindTypes = array(
      "integer" => Db_Table_Binding::TYPEINT
   );
}

【问题讨论】:

  • Db_Table_Data_Binding 类是否有带参数的构造函数?
  • Db_table_Data_Binding 是否扩展 Db_Table_Binding
  • 不,不,即使我为简洁起见将其从代码中省略了,也检查了上面的构造函数 if($reflect->getConstructor()))
  • Rut Roe... 你刚刚让我意识到我引用了一个错误的类常量,并且作为“Db_Table_Binding,抱歉又浪费大家的时间在我的noobery上。

标签: php class reflection fatal-error


【解决方案1】:

这可能是因为你的类的构造函数使用了 Db_Table_Binding 类并且它在不同的命名空间中,并且你的自动加载器找不到类定义。

尝试在脚本开头编写:

use NamespeceWhereUGotThisClass\Db_Table_Binding;

【讨论】:

  • 我没有/不使用 PHP 中的命名空间。我从 db_table_binding 引用了一个类常量,并试图在我的 db_table_data_binding 类的属性数组中分配它。土豆动起来。
猜你喜欢
  • 2017-02-11
  • 2012-10-02
  • 2012-01-10
  • 1970-01-01
  • 2014-04-03
  • 1970-01-01
  • 2020-08-13
  • 2022-08-11
  • 1970-01-01
相关资源
最近更新 更多