暴露“使用"包含文件的类 [英] Expose "use" classes to Included file

查看:20
本文介绍了暴露“使用"包含文件的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例子:

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.

这篇关于暴露“使用"包含文件的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆