問題描述
我有一個(gè)帶有 Resp
類的文件.路徑是:
I have a file with a class Resp
. The path is:
C:xampphtdocsOneClassesResp.php
我在這個(gè)目錄中有一個(gè) index.php
文件:
And I have an index.php
file in this directory:
C:xampphtdocsTwoHttpindex.php
在這個(gè) index.php
文件中,我想實(shí)例化一個(gè) Resp
類.
In this index.php
file I want to instantiate a class Resp
.
$a = new Resp();
我知道我可以使用 require
或 include
關(guān)鍵字將文件包含在一個(gè)類中:
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
的情況下導(dǎo)入類.我試圖了解 use
關(guān)鍵字的工作原理.我嘗試了這些步驟,但沒有任何效果:
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
關(guān)鍵字use
是如何工作的?我可以用它來導(dǎo)入課程嗎?
How does the keyword use
work? Can I use it to import classes?
推薦答案
use
不包含任何內(nèi)容.它只是將指定的命名空間(或類)導(dǎo)入到當(dāng)前作用域
use
doesn't include anything. It just imports the specified namespace (or class) to the current scope
如果您希望自動加載類 - 閱讀關(guān)于自動加載
If you want the classes to be autoloaded - read about autoloading
這篇關(guān)于關(guān)鍵字“使用"如何?在 PHP 中工作,我可以用它導(dǎo)入類嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!