关键字“使用"如何?在 PHP 中工作,我可以用它导入类吗?
我有一个带有 Resp
类的文件.路径是:
I have a file with a class Resp
. The path is:
C:xampphtdocsOneClassesResp.php
我在这个目录中有一个 index.php
文件:
And I have an index.php
file in this directory:
C:xampphtdocsTwoHttpindex.php
在这个 index.php
文件中,我想实例化一个 Resp
类.
In this index.php
file I want to instantiate a class Resp
.
$a = new Resp();
我知道我可以使用 require
或 include
关键字将文件包含在一个类中:
I know I can use require
or include
keywords to include the file with a class:
require("OneClassesResp.php"); // I've set the include_path correctly already ";C:xampphtdocs". It works.
$a = new Resp();
但我想在不使用 require
或 include
的情况下导入类.我试图了解 use
关键字的工作原理.我尝试了这些步骤,但没有任何效果:
But I want to import classes without using require
or include
. I'm trying to understand how use
keyword works. I tried theses steps but nothing works:
use OneClassesResp;
use xampphtdocsOneClassesResp;
use htdocsOneClassesResp;
use OneClasses;
use htdocsOneClasses; /* nothing works */
$a = new Resp();
它说:
Fatal error: Class 'OneClassesResp' not found in C:xampphtdocsTwoHttpindex.php
关键字use
是如何工作的?我可以用它来导入课程吗?
How does the keyword use
work? Can I use it to import classes?
推荐答案
use
不包含任何内容.它只是将指定的命名空间(或类)导入到当前作用域
use
doesn't include anything. It just imports the specified namespace (or class) to the current scope
如果您希望自动加载类 - 阅读关于自动加载
If you want the classes to be autoloaded - read about autoloading
相关文章