【问题标题】:Importing class without namespace to namespaced class将没有命名空间的类导入命名空间类
【发布时间】:2011-12-20 11:36:03
【问题描述】:

我有一个类,它包括 Smarty,但我的类使用命名空间测试,Smarty 不使用命名空间。 如何包含 Smarty,而不将命名空间写入 smarty 文件(它有许多系统插件)

    import "smarty/Smarty.php"

    class testik
    {
        public function __construct ()
        {
            $smarty = new Smarty();
        }
    }


<?php

    class Smarty
    {
        //somcode
    }

Smarty 有自动加载器类并包含它的插件,插件也没有命名空间。

【问题讨论】:

    标签: php namespaces smarty3


    【解决方案1】:

    告诉你的命名空间代码它在全局命名空间中:

    $smarty = new \Smarty();
    

    另外importing­Docs 也是这样工作的:

    use Smarty;
    

    然后您可以按原样使用您的代码:

    $smarty = new Smarty();
    

    另请参阅:How to use “root” namespace of php?

    【讨论】:

    • 大声笑,我与我的命名空间、目录、自动加载器进行了斗争,但忘记了这个
    • 当您需要调用外部静态方法/变量在命名空间类中时也可以使用,例如\Handy::doGeneralStuff();
    猜你喜欢
    • 2013-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-08
    • 1970-01-01
    • 2016-12-03
    • 1970-01-01
    相关资源
    最近更新 更多