简体   繁体   English

在包含文件上使用名称空间

[英]Using namespace on include file

I have a class: 我有一堂课:

namespace Navi\View;

use Navi\Navi;

class View extends Base{
    public function render(){
        $test = 'This is a local variable!';
        $obj = new Navi; //Working, ofcourse!
        include 'file.php';
    }
}

And in "file.php"; 并在“ file.php”中;

<?php
echo $test;
var_dump(new Navi); //Class Navi not found

Why the local $test variable pass to file.php, but Navi class isn't? 为什么本地$ test变量传递给file.php,但Navi类不是?

Ofcourse, if I use "use Navi\\Navi" in "file.php" then code working. 当然,如果我在“ file.php”中使用“ use Navi \\ Navi”,则代码可以正常工作。 I don't understand why! 我不明白为什么!

Any way to using Navi class that not using "use Navi\\Navi"? 有什么方法可以使用不使用“ use Navi \\ Navi”的Navi类?

Please help me! 请帮我!

Thankyou! 谢谢!

Importing rules are per file basis, meaning included files will NOT inherit the parent file's importing rules. 导入规则是基于文件的,这意味着包含的文件将不会继承父文件的导入规则。

I've go to change your code in file.php this way: 我将通过以下方式更改file.php中的代码:

var_dump(new \Navi\Navi)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM