2014-04-02 31 views
0

在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 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 
?> 

從這裏:http://www.php.net/manual/en/language.namespaces.importing.php

他們怎麼能在這個聲明中說$obj = new namespace\Another;它從類foo\another實例化一個對象?當我試圖定義添加到類Another我是說,另一個名字是已經在使用(因爲它是一個別名)的錯誤。

回答

0

這如果foo\Another類在另一個文件中定義沒有這樣的別名工作。

another.php

<?php 

namespace foo; 

class Another { } 

test.php的

<?php 

namespace foo; 
use My\Full\Classname as Another; 

require_once 'another.php'; 

new namespace\Another; 

給出的例子是爲了說明的命名空間分辨率和方便地忽略了其他細節和可能出現的衝突。