【问题标题】:Symfonys Autoloader - Implement Parent NamespacesSymfonys Autoloader - 实现父命名空间
【发布时间】:2013-03-04 15:09:14
【问题描述】:

我正在将 Symfonys Autoloader 用于具有以下文件夹/类结构的项目:

App
+- Package1
|
+- Package2
  +- Class1.php
|
- Interface1.php

我现在如何从父命名空间实现一个类。 比如Interface1 来自Class1。 这不起作用:

namespace App\Package1

Class1 implements App\Interface1
{
    //implement some functions here...
}

然后自动加载器尝试包含App\Package2\Class1\App\Interface1

最好的问候,

曼努埃尔

【问题讨论】:

    标签: php class symfony namespaces autoloader


    【解决方案1】:

    symfony 的类加载器效果很好:)

    在这种情况下,这是一个php语法问题。

    你需要使用“use”来导入命名空间,或者更简单的是你只需要在类名前面加一个反斜杠

    例如:

    namespace App\Package1;
    
    use App\Interface1;
    
    Class1 implements Interface1
    {
        //implement some functions here...
    }
    

    namespace App\Package1;
    
    Class1 implements \App\Interface1;
    {
        //implement some functions here...
    }
    

    【讨论】:

    • 感谢您的帮助:)
    猜你喜欢
    • 2012-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    • 2018-06-17
    • 2015-04-17
    • 2016-03-02
    相关资源
    最近更新 更多