2017-08-10 54 views
0

我遇到的问题是当我调用类MySpices方法getSpice时,我什么都没有,没有错误,没有任何回应。 我感觉问题在使用命名空间内。所有其他类的工作正常,除了这个类MySpices是唯一扩展的文件。任何建议都会有所帮助。PHP - 在命名空间文件中使用PSR-4自动加载作曲程序的命名空间

这里是我的显示目录和文件的样本:

// files and directories 
    site.com 
     |-meatballs 
     | |--src 
     | | |-app 
     | | |-errors 
     | | |-include 
     | | |-library 
     | |  |-MySpices.php 
     | |  |-SpiceFac.php 
     | |  |--more files 
     | |--vendor 
     | | |-composer 
     | | |-autoload.php 
     | | 
     | composer.json 
     | 
    index.php 

的composer.json文件包括:

{ 
     "autoload": { 
      "psr-4": { 
       "meatballs\\": "src/" 
      } 
     } 
    } 

文件名:MySpices.php

namespace meatballs\library 
    { 
     use meatballs\library as QDSE; 

     class MySpices extends QDSE\SpiceFac 
     { 
     protected $_spice; 

     function __construct() 
     { 
      parent::__construct(); 

      $this->_spice = 'ginger'; 
      // more codes 

     } 

     public function getSpice() 
     { 
      return $this->_spice; 
     } 
     } 
    } 

文件名:SpiceFac。 php

namespace meatballs\library 
    { 
     class SpiceFac 
     { 
     protected $_spices; 

     function __construct() 
     { 
      // some codes here  
     } 

     private function spiceList() 
     { 
      // spices arrays 
      $this->_spaces = $spices; 
     } 

     public function getSpices() 
     { 
      return $this->_spaces; 
     } 

     // other class methods 
     } 
    } 

index.php页面

// get the apps autoload file 
    $loadFiles = '../vendor/autoload.php'; 

    // check if autoloader file exist 
    if(file_exists($loadFiles)) 
     require_once $loadFiles; 

    // use namespace 
    use meatballs\library\SpiceFac; 
    use meatballs\library\MySpices; 

    $mySpice = new MySpices(); 
    $taste = $mySpice->getSpice(); 

    echo $taste . "<br />"; 
+0

发现问题并已更正 –

+0

因此您已发现问题,对不对? – Matteo

回答

0

添加一个别名来使用命名空间解析与类MySpice问题。