【问题标题】:PHP class not found when using namespace使用命名空间时找不到 PHP 类
【发布时间】:2016-08-15 16:28:31
【问题描述】:

我是这个命名空间的新手。

我的基本目录中有 2 个类(单独的文件),例如 class1.phpclass2.php 在目录 src/ 中。

class1.php

namespace \src\utility\Timer;

class Timer{
    public static function somefunction(){

    }
}

class2.php

namespace \src\utility\Verification;
use Timer;

class Verification{
     Timer::somefunction();
}

当我执行class2.php 时,我得到了致命错误

PHP 致命错误:在 path/to/class2.php 中找不到类 'Timer' 行***

我在某处读到 SO,我需要为此创建自动加载器。如果是这样,我该如何创建一个,如果不是,那还有什么问题?

更新

我创建了一个自动加载器,它将require 我的 php 脚本之上的所有必需文件。 所以,现在 class2.php 会变成这样。

namespace \src\utility\Verification;
require '/path/to/class1.php'
use Timer;
//or use src\utility\Timer ... both doesn't work.

class Verification{
     Timer::somefunction();
}

这也不起作用,它显示未找到该类。但是,如果我删除所有namespacesuse。一切正常。

【问题讨论】:

  • 在您的 class2.php 中尝试使用 \src\Timer\Timer 作为计时器;
  • @ManinderpreetSingh 试过了。同样的问题。
  • 我认为您应该查看Composer。命名空间只是类的唯一标识符。当一个类被实例化但它不存在但在预处理器抛出 T_FATAL 错误之前,PHP 的自动加载会被重载。通常的做法是然后将\ 替换为/ 并从文件夹结构中加载它(为什么命名空间通常与目录结构匹配。
  • @ash 你能简单地告诉我 Composer 将如何帮助我的项目吗?我需要使用命名空间。我希望你已经阅读了我在问题中的更新。
  • 它不会修复您的命名空间,它可以帮助您在代码中实例化类时加载类。结帐PHP The Right Way

标签: php namespaces autoload spl-autoload-register


【解决方案1】:

您将需要实现一个自动加载器,因为您已经在 SO 中阅读过它。

您可以在 http://www.php-fig.org/psr/psr-4/ 查看自动加载标准 PSR-4,您可以在 https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md 看到 PSR-4 自动加载的示例实现和处理多个命名空间的示例类实现。

【讨论】:

  • 我明白了。事实上,我现在正在阅读这些内容。这意味着,关键字namespaceuse 在没有自动加载器的情况下有点没用?
  • 命名空间只是文档所说的封装项目的一种方式
【解决方案2】:

我们可以通过两种方式解决命名空间问题

1) We can just use namespace and require

2) We can use Composer and work with the autoloading!

第一种方式(命名空间和要求)方式

Class1.php(定时器类)

namespace Utility;

class Timer
   {
    public static function {}
   }

Class2.php(验证类)

namespace Utility;
require "Class1.php";

//Some interesting points to note down!
//We are not using the keyword "use" 
//We need to use the same namespace which is "Utility" 
//Therefore, both Class1.php and Class2.php has "namespace Utility"

//Require is usually the file path!
//We do not mention the class name in the require " ";
//What if the Class1.php file is in another folder?
//Ex:"src/utility/Stopwatch/Class1.php"  

//Then the require will be "Stopwatch/Class1.php"
//Your namespace would be still "namespace Utility;" for Class1.php

class Verification
   {
     Timer::somefunction();
   }

第二种方式(使用 Composer 和自动加载方式)

制作 composer.json 文件。根据您的示例“src/Utility” 我们需要在 src 文件夹之前创建一个 composer.json 文件。示例:在名为 myApp 的文件夹中,您将拥有 composer.json 文件和一个 src 文件夹。

   {
     "autoload": {
     "psr-4": {
        "Utility\\":"src/utility/"
        }
     }   
   }

现在转到该文件夹​​,在 composer.json 文件所在的文件夹位置打开终端。现在输入终端!

   composer dump-autoload

这将创建一个供应商文件夹。因此,如果您有一个名为“MyApp”的文件夹 您将看到 vendor 文件夹、src 文件夹和 composer.json 文件

Timer.php(定时器类)

namespace Utility;

class Timer
     {
      public static function somefunction(){}
     }

Verification.php(验证类)

namespace Utility; 
require "../../vendor/autoload.php"; 
use Utility\Timer; 

class Verification
  {
     Timer::somefunction(); 
  }

当你有一个复杂的文件夹结构时,这个方法更强大!!

【讨论】:

    猜你喜欢
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    • 1970-01-01
    • 2012-10-23
    • 2017-05-01
    • 1970-01-01
    相关资源
    最近更新 更多