2011-12-23 133 views
4

我仍然遇到PHP5命名空間的問題。不能在PHP的另一個命名空間中使用命名空間類

我有一個命名空間稱爲Project,我試圖訪問此Project命名空間有Library一個命名空間,以便在該文件的頂部是一個Project命名空間我使用這行內稱爲registryuse Library\Registry;

Registry類是Library命名空間內

這應該工作,但它並沒有,而不是唯一的方式來訪問我的registry類這裏面Project命名空間是使用此

$this->registry = new \Library\Registry; 

我希望能夠改爲使用此...

$this->registry = new Registry; 

這是使用

use Library\Registry; 

Project空間文件的頂部的全部原因


下面我有3個小考試在這樣的文件夾結構中運行腳本。
Library/registry.class.php在我的庫文件夾
Controller/controller.class.php和類在我的控制器目錄
Controller/testing.php測試文件來運行該腳本的類。

E:\圖書館\ Registry.class.php文件

<?php 
namespace Library 
{ 
    class Registry 
    { 
     function __construct() 
     { 
      echo 'Registry.class.php Constructor was ran'; 
     } 
    } 
} 
?> 

E:\控制器\ Controller.class.php文件

<?php 
use Library\Registry; 

namespace Project 
{ 
    class Controller 
    { 
     public $registry; 

     function __construct() 
     { 
      include('E:\Library\Registry.class.php'); 

      // This is where my trouble is 
      // to make it work currently I have to use 
      // $this->registry = new /Library/Registry; 
      // But I want to be able to use it like below and that is why 
      // I have the `use Library\Registry;` at the top 
      $this->registry = new Registry; 
     } 

     function show() 
     { 
      $this->registry; 
      echo '<br>Registry was ran inside testcontroller.php<br>'; 
     } 
    } 
} 
?> 

E:\控制器\ testing.php文件

<?php 
use Project\Controller; 

include('testcontroller.php'); 

$controller = new Controller(); 
$controller->show(); 

?> 

我得到這個錯誤...

Fatal error: Class 'Project\Registry' not found in PATH to file 

,除非我因爲該文件在頂部使用下面這個就controller.class.php文件

$this->registry = new \MyLibrary\Registry; 

我有use Library\Registry;我應該能夠像這樣訪問它...

$this->registry = new Registry; 

請幫我得到它,我可以用它這樣的,而不是

回答

5
use Library\Registry; 

namespace Project 
{ 

相信這一輪是錯誤的方式:你是use荷蘭國際集團Library\Registry在全局命名空間,然後打開Project命名空間。

use語句放入要導入的命名空間中。

namespace Project 
{ 
    use Library\Registry; 
+0

其實文檔說你不能這樣做,如果你在命名空間設置後使用'use',它會導致一個致命的錯誤。在我的例子中,它導致了這個'分析錯誤:語法錯誤,意外的T_CLASS,期待','或';'在第8行的E:\ Controller \ controller.class.php中# – JasonDavis 2011-12-23 14:51:59

+0

@jasondavis不,那是因爲我忘了在分隔符的末尾加上分號。試用我編輯的版本。 (順便說一句,這應該是相當明顯的錯誤...) – lonesomeday 2011-12-23 14:58:44

+0

你說得對,我應該發現。它似乎在工作,這是否應該始終這樣做?命名空間聲明之後?每當我想我完全用這些命名空間獲得它,彈出一些不起作用的命令空間,我就應該說我的麻煩開始了。謝謝你的幫助。在看C#項目時,他們是這樣做的,所以我認爲這會增加混淆 – JasonDavis 2011-12-23 15:10:55

1

只需添加: 使用\庫\註冊表;

在命名空間聲明在你的腳本的頂部

然後,你可以說:

$註冊表=新登記;

裏面順便說你的類聲明是完全錯誤的。你不應該把你的名字空間包裹在大括號中,名字空間不是一個函數。

這是應該的。還要確保已使用include('/ path/to/registry.php')包含了Library \ Registry的類聲明。或使用自動加載器

namespace Project; 

include('E:\ Library \ Registry.class.php');

use \Library\Registry; 

    class Controller 
    { 
     public $registry; 

     function __construct() 
     { 


      // This is where my trouble is 
      // to make it work currently I have to use 
      // $this->registry = new /Library/Registry; 
      // But I want to be able to use it like below and that is why 
      // I have the `use Library\Registry;` at the top 
      $this->registry = new Registry; 
     } 

     function show() 
     { 
      $this->registry; 
      echo '<br>Registry was ran inside testcontroller.php<br>'; 
     } 
    } 

享受

+0

不要你看我已經在controller.class.php文件 – JasonDavis 2011-12-23 14:43:32

+0

沒有什麼不好的,在花封閉你的命名空間括號。 [看手冊](http://www.php.net/manual/en/language.namespaces.definitionmultiple.php) – lonesomeday 2011-12-23 14:47:58

+0

對不起,我真的不知道用大括號包裝命名空間,只是從來沒有看到任何人這樣做。 – 2011-12-23 14:56:01

2

您需要導入內部Project命名空間的Registry類,因爲你需要它們存在,而不是在全球範圍內。

<?php 
namespace Project 
{ 
    use Library\Registry; 

    class Controller 
    { 
     public $registry; 

     function __construct() 
     { 
      include('E:\Library\Registry.class.php'); 

      // This is where my trouble is 
      // to make it work currently I have to use 
      // $this->registry = new /Library/Registry; 
      // But I want to be able to use it like below and that is why 
      // I have the `use Library\Registry;` at the top 
      $this->registry = new Registry; 
     } 

     function show() 
     { 
      $this->registry; 
      echo '<br>Registry was ran inside testcontroller.php<br>'; 
     } 
    } 
} 
?> 
0
<?php namespace Project; 
    require_once 'your registry class file'; 
    use \Library\Registry as Registry; 

現在,您將能夠使用...

$this->registry = new Registry; 
相關問題