[英]So how do I make a class name global in PHP?
I have a class called "user" with a function "createAccount". 我有一个名为“用户”的类,具有一个函数“ createAccount”。 Within it I like to access another class via PostageApp::mail
我喜欢在其中通过PostageApp :: mail访问另一个类
But as this class is outside the scope it is not accessible. 但是由于此类不在范围内,因此无法访问。
global
seams to work for variables only. global
接缝仅适用于变量。 So how do I make a class name global? 那么,如何使一个类名全局?
include_once('../../ext_scripts/postageapp_class.inc');
class user {
function createAccount(...) {
global PostageApp;
[...]
$result = PostageApp::mail($mailTo, '', 'verify_email', array());
}
}
The class have to be defined before you use it, so you have use require
or require_once
to include the file with your PostageApp
class definition. 使用该类之前必须先对其进行定义,因此必须使用
require
或require_once
将文件包含在PostageApp
类定义中。
There is no notion of global for classes. 对于类,没有全局的概念。
If you don't want to add the require
, I advise you to look into autoloading . 如果您不想添加
require
,建议您考虑自动加载 。
If you are absolutely sure that the class has been defined in an included file, there is only one thing affecting its visibility in another file: I suppose the PostageApp
class is in another namespace , as this is the only reason another class could have a different scope in PHP. 如果您绝对确定已在包含文件中定义了该类,则只有一件事会影响其在另一个文件中的可见性:我认为
PostageApp
类位于另一个名称空间中 ,因为这是另一个类可能具有不同的唯一原因。 PHP中的作用域。
The keyword global
is only necessary for global variables. 关键字
global
仅对于全局变量是必需的。 Functions and classes are always accessible in any scope as soon as they have been declared. 函数和类一经声明,便始终可以在任何范围内访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.