2016-11-19 118 views
0

我特林申報接口,我的課是這樣的:PHP中的接口聲明 - 爲什麼聲明混合?

namespace Helpers\Interfaces { 
    interface Cache { 
     public static function getInstance($profile = null); 
    } 
} 

然後我申請他們是這樣的:

namespace Helpers { 
    class Cache implements Interfaces\Cache { 
     public static function getInstance($profile = null) { 
      /* ... */ 
     } 
    } 
} 

到目前爲止,一切都很好(顯然,至少)。我有的問題是NetBeans給我一個錯誤,指出我的類不是抽象的,也沒有實現某種方法。

該方法屬於我創建的一個對象,用於收集操作某些方法所需的配置參數,而無需根據對象(如主機,端口,API密鑰等)提供特定的配置選項。

這種方法,在這個例子中,被稱爲\Configuration\Helpers\Cache::getConfiguration($profile);

有衝突的聲明來源於此接口:

namespace Configuration\Helpers\Interfaces { 
    interface Cache { 
     public static function getConfiguration($profile = null); 
    } 
} 

其中應用如下:

namespace Configuration\Helpers { 
    class Cache implements Interfaces\Cache { 
     public static function getConfiguration($profile = null) { 
      /* ... */ 
     } 
    } 
} 

它有效地混合界面,儘管它們是命名空間! 我想說的是,接口和實現這種接口的類聲明總是在同一個文件中,每個對象一個文件。

NetBeans 8.2上的PHP版本是7.0.13。

我在做什麼錯?

回答

0

您的接口和類位於不同的名稱空間中。您可能需要爲您的班級添加使用說明。例如,你可以添加:使用Helpers \ Interfaces作爲接口。該行可以超出類定義:class Cache實現Interfaces \ Cache。

另請參閱:http://php.net/manual/en/language.namespaces.definitionmultiple.php

+0

我會試試看。感謝提示:) –

+0

不,它不會使錯誤通知消失。但是,我懷疑它與NetBeans相關,而不是我的編碼風格,因爲我在執行代碼時沒有任何警告或錯誤,只是NetBeans抱怨聲明... –