Php 名称类似于类名的别名
在php.net中有一个我不理解的示例:Php 名称类似于类名的别名,php,class,namespaces,alias,Php,Class,Namespaces,Alias,在php.net中有一个我不理解的示例: <?php namespace foo; use My\Full\Classname as Another; // this is the same as use My\Full\NSname as NSname use My\Full\NSname; // importing a global class use ArrayObject; $obj = new namespace\Another; // instantiates objec
<?php
namespace foo;
use My\Full\Classname as Another;
// this is the same as use My\Full\NSname as NSname
use My\Full\NSname;
// importing a global class
use ArrayObject;
$obj = new namespace\Another; // instantiates object of class foo\Another
$obj = new Another; // instantiates object of class My\Full\Classname
NSname\subns\func(); // calls function My\Full\NSname\subns\func
$a = new ArrayObject(array(1)); // instantiates object of class ArrayObject
// without the "use ArrayObject" we would instantiate an object of class foo\ArrayObject
?>
从这里开始:
他们怎么能在这个语句中说$obj=newnamespace\other;它从类foo\other实例化一个对象?当我尝试将定义添加到另一个类时,我遇到了一个错误,该错误表示另一个名称已在使用,因为它是一个别名。如果在另一个文件中定义了没有此类别名的foo\other类,则此操作有效
另一个.php
test.php
给出的示例旨在说明名称空间解析,并方便地忽略可能出现的其他细节和冲突
<?php
namespace foo;
class Another { }
<?php
namespace foo;
use My\Full\Classname as Another;
require_once 'another.php';
new namespace\Another;