使用名称空间时找不到PHP类 [英] PHP class not found when using namespace
问题描述
我是这个命名空间的新手.
I am new with this namespace thing.
我的基本目录中有2个类(单独的文件),例如目录src/
中的class1.php
和class2.php
.
I have 2 classes(separate files) in my base directory, say class1.php
and class2.php
inside a directory 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" ***行
PHP Fatal error: Class 'Timer' not found in path/to/class2.php at line ***
我在SO上的某处读到了我需要为此创建Autoloader的信息.如果是这样,我该如何创建一个,如果不是,那么还有什么问题呢?
I read somewhere on SO, that I need to create Autoloaders for this. If so, how do I approach into creating one, and if not, then what else is the issue?
更新
我创建了一个自动加载器,它将在我的PHP脚本顶部require
所有必需的文件.
因此,现在class2.php最终会像这样.
I created an Autoloader which will require
all the required files on top of my php script.
So, now the class2.php would end up like this.
namespace \src\utility\Verification;
require '/path/to/class1.php'
use Timer;
//or use src\utility\Timer ... both doesn't work.
class Verification{
Timer::somefunction();
}
这也不起作用,它表明找不到该类.但是,如果我删除所有的namespaces
和use
.一切正常.
This also does not work, and it shows that class not found. But, if I remove all the namespaces
, and use
's. Everything works fine.
推荐答案
我们可以通过两种方式解决名称空间问题
We can solve the namespace problem in two ways
第一种方式(命名空间和要求)
Class1.php(计时器类)
Class1.php (Timer Class)
namespace Utility;
class Timer
{
public static function {}
}
Class2.php(验证类)
Class2.php (Verification Class)
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文件夹.
Make composer.json file. According to your example "src/Utility" We need to create a composer.json file before the src folder. Example: In a folder called myApp you will have composer.json file and a src folder.
{
"autoload": {
"psr-4": {
"Utility\\":"src/utility/"
}
}
}
现在转到该文件夹,在有composer.json文件的文件夹位置中打开您的终端.现在输入终端!
Now go to that folder open your terminal in the folder location where there is composer.json file. Now type in the terminal!
composer dump-autoload
这将创建一个供应商文件夹.因此,如果您有一个名为"MyApp"的文件夹 您将看到供应商文件夹,src文件夹和composer.json文件
This will create a vendor folder. Therefore if you have a folder named "MyApp" you will see vendor folder, src folder and a composer.json file
Timer.php(计时器类)
Timer.php(Timer Class)
namespace Utility;
class Timer
{
public static function somefunction(){}
}
Verification.php(验证类)
Verification.php (Verification Class)
namespace Utility;
require "../../vendor/autoload.php";
use Utility\Timer;
class Verification
{
Timer::somefunction();
}
当您具有复杂的文件夹结构时,此方法将更强大!!
This method is more powerful when you have a complex folder structure!!
这篇关于使用名称空间时找不到PHP类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!