简体   繁体   English

致命错误:找不到“ App \\ PDO”类

[英]Fatal error: Class 'App\PDO' not found in

I get this error when I try to use namespaces. 尝试使用名称空间时出现此错误。

I have 我有

namespace App;

the top, and the class looks like 顶部,班级看起来像

class database{

  function __construct(..)
    try{
      $this->db = new PDO(...)   <-- here the error
    ...
  }
}

I dont' understand how do namespaces work? 我不了解名称空间如何工作? Shouldn't PHP fallback to the default PDO class if app/PDO is not found? 如果未找到app / PDO,PHP是否应该回退到默认的PDO类?

Shouldn't PHP fallback to the default PDO class if app/PDO is not found? 如果未找到app / PDO,PHP是否应该回退到默认的PDO类?

No, it should not. 不,不应该。

From the documentation : 文档中

Class names always resolve to the current namespace name. 类名称始终解析为当前名称空间名称。 Thus to access internal or non-namespaced user classes, One must refer to them with their fully qualified Name 因此,要访问内部或非命名空间的用户类, 必须使用全限定名称来引用它们

For your specific example, the fully qualified name for PDO would be \\PDO . 对于您的特定示例, PDO的完全限定名称为\\PDO

Just add use PDO; 只需添加use PDO; after the namespace and before the class. 在名称空间之后,在类之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM