暴露“使用"包含文件的类

2022-01-14 00:00:00 namespaces include php

例子:

namespace Somenamespace;    
use SomenamespaceSomeclass;
use Somenamespaceotherclass;


class Template{

  public function display($templ){
    load_template($templ);
  }

}


function load_template($file){
  unset($file);
  require func_get_arg(0);
}

$template = new Template();
$template->display('file.php');

现在我想访问 file.php 中的Someclass",而不必先在use"语句中声明它.例如.someclass::dostuff();(没有命名空间)

Now I want to access "Someclass" in file.php, without having to declare it first in the "use" statement. eg. someclass::dostuff(); (without the namespace)

有可能吗?

推荐答案

简单地说:没有.请参阅示例 http://www.php 下方的注释.net/manual/en/language.namespaces.importing.php#example-247

Simply: no. See note bellow the example http://www.php.net/manual/en/language.namespaces.importing.php#example-247

导入规则基于每个文件,这意味着包含的文件将不继承父文件的导入规则.

Importing rules are per file basis, meaning included files will NOT inherit the parent file's importing rules.

相关文章