Should be a simple question. I do not yet have 5.3 installed, so I cannot experiment myself.
When declaring a namespace in an included file, do I have to declare the full path of the namespace, or is the parent namespace already assumed to be included?
For instance, if I have a file:
// file1.php
&l开发者_运维技巧t;?php
namespace parent_space;
include 'file2.php';
?>
and a second file:
// file2.php
<?php
namespace child_space;
// some code
?>
Since file2.php
is included from within the parent_space
namespace in file1.php
, is the namespace for "some code" \parent_space\child_space\
, or is it just \child_space\
?
No. PHP does not attach any significance to the location of your file when including, but it does treat each file as a completely separate entity as far as namespaces are concerned when parsing the code.
So this code will not work:
<?php
namespace Food; //this is a top level namespace
include 'file2.php';
//file2.php
<?php
namespace Tacos; //this is still a top level namespace
You would need to define your file2.php in this manner:
<?php
namespace Food\Tacos;
Reference the PHP manual for more information about namespaces: http://www.php.net/manual/en/language.namespaces.basics.php
There are some similar examples in the PHP manual namespace section:
http://www.php.net/manual/en/language.namespaces.basics.php
The short answer is: no.
精彩评论