Does the namespace declarations inside the included files automatically include the namespace of the parent file (PHP)?

There should be a simple question. I don't have 5.3 yet, so I can't experiment myself.

When declaring a namespace in an included file, is it necessary to declare the full namespace path, or is it the parent namespace that is supposed to be included?

For example, if I have a file:

// file1.php
<?php
    namespace parent_space;
    include 'file2.php';
?>

and second file:

// file2.php
<?php
    namespace child_space;
    // some code
?>

Since it is file2.phpincluded from the namespace parent_spacein file1.php, is it a namespace for "some code" \parent_space\child_space\, or is it simple \child_space\?

+3
source share
2 answers

. PHP , .

, :

<?php
    namespace Food; //this is a top level namespace
    include 'file2.php';

//file2.php
<?php
    namespace Tacos; //this is still a top level namespace

file2.php :

<?php

namespace Food\Tacos;

PHP : http://www.php.net/manual/en/language.namespaces.basics.php

+3

Source: https://habr.com/ru/post/1613890/


All Articles