How does the keyword quot;usequot; work in PHP and can I import classes with it?(关键字“使用如何?在 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
这篇关于关键字“使用"如何?在 PHP 中工作,我可以用它导入类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:关键字“使用"如何?在 PHP 中工作,我可以用
基础教程推荐
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何替换eregi() 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
