2012-06-25 3 views
0
<?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 
?> 

도와주세요. use My\Full\Classname as Another;네임 스페이스는 PHP에서

+0

그 그냥 그게 전부 foo' 네임 스페이스의'Another''라는 클래스를 사용하는 말. – uday

답변

1

의 의미는 무엇

별칭을 이잖아. 당신이 (상대적으로) 네임 스페이스 또는 클래스 명으로 Another을 참조 할 때마다, 그것은 네임 스페이스 분리 \로 시작 \My\Full\Classname

$x = new Another; 
echo get_class($x); // "\My\Full\Classname" 
$y = new Another\Something; 
echo get_class($y); // "\My\Full\Classname\Something" 

식별자로 해석 정규화 된 이름입니다됩니다. 식별자가없는 경우 식별자는 현재 네임 스페이스와 use (이 순서로)에 정의 된 별칭 정의에 대해 확인됩니다 (식별자는 usenamespace 제외 : 항상 정규화되어 있음).

PHP-Manual: Namespaces

+0

그래서 .. \ My \ Full은 네임 스페이스이고 Classname은 네임 스페이스의 클래스입니까? –

+0

그것은 달려있다 :'My \ Full \ Classname as Another를 사용하라. '라는 종류의 접두사 별칭. 두 경우 모두'new another;'또는'new Another \ Something \ Different'를 입력하면'Another '를 정의한 접두사 ('\ My \ Full \ Classname' 또는' \ My \ Full \ Classname \ Something \ Different'). – KingCrunch

+0

''다른 이름으로 \ My \ Full \ Classname을 사용하십시오 '에있는'Classname'이 클래스가 아닌 경우 어떻게 우리가 Another ('new Another;') 객체를 만들 수 있습니까? –