创建对象时的反斜杠语法 [英] Backslash syntax when creating objects
问题描述
require和require_once中的路径类似于(dir1/dir2/test.php).
我们可以创建与$obj= new class1/class2;
相同的对象吗?
如果是,请解释.
The path in require and require_once is like (dir1/dir2/test.php).
Can we create objects the same like $obj= new class1/class2;
?
If yes, please explain.
http://php-fedex-api- wrapper.googlecode.com/svn/trunk/htdocs/example1.php
$rateRequest = new ComplexType\RateRequest();
推荐答案
It is not using the path, it is using the namespace (ComplexType
); a feature built-into PHP 5.3.
更多信息:
If however, you want to autoload certain classes, take a look at __autoload
magic function.
许多开发人员在写作 面向对象的应用程序创建 每个类一个PHP源文件 定义.最大的之一 烦恼不得不写很长的篇幅 所需的清单包括在 每个脚本的开头(每个脚本一个 类).
Many developers writing object-oriented applications create one PHP source file per-class definition. One of the biggest annoyances is having to write a long list of needed includes at the beginning of each script (one for each class).
在PHP 5中,这不再是必需的. 您可以定义__autoload函数 如果出现这种情况,则会自动调用 您正在尝试使用 尚未的类/接口 尚未定义.通过调用此函数 脚本引擎获得了最后 有机会在PHP之前加载类 失败并显示错误.
In PHP 5, this is no longer necessary. You may define an __autoload function which is automatically called in case you are trying to use a class/interface which hasn't been defined yet. By calling this function the scripting engine is given a last chance to load the class before PHP fails with an error.
示例:
function __autoload($class_name) {
include $class_name . '.php';
}
$obj = new MyClass1();
$obj2 = new MyClass2();
这篇关于创建对象时的反斜杠语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!