問題描述
我有一個帶有 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
這篇關于關鍵字“使用"如何?在 PHP 中工作,我可以用它導入類嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!