2015-04-02 74 views
0

您好,我嘗試在我的laravel項目中包含某個文件中的某些類時遇到了一個奇怪的問題。這是文件:找不到類/無法重新聲明類

<?php namespace Libraries\MPowerLib; 

require("mpower/dependency_check.php"); 

set_include_path(get_include_path() . PATH_SEPARATOR . realpath(dirname(__FILE__))); 

abstract class MPower { 
    const VERSION = "1.2.0"; 
} 

if (strnatcmp(phpversion(),'5.3.0') >= 0) { 
    define('JSON_ENCODE_PARAM_SUPPORT', true); 
}else{ 
    define('JSON_ENCODE_PARAM_SUPPORT', false); 
} 

require_once("mpower/setup.php"); 
require_once("mpower/customdata.php"); 
require_once("mpower/checkout.php"); 
require_once("mpower/checkout/store.php"); 
require_once("mpower/checkout/checkout_invoice.php"); 
require_once("mpower/checkout/onsite_invoice.php"); 
require_once("mpower/direct_pay.php"); 
require_once("mpower/direct_card.php"); 
require_once("mpower/libraries/Requests.php"); 
require_once("mpower/utilities.php"); 

現在,當我使用require_once我得到:

Class 'Libraries\MPowerLib\MPower_Checkout_Invoice' not found 

然而,當我只是需要它的工作原理使用,但我不斷收到此錯誤:

Cannot redeclare class libraries\mpowerlib\mpower_checkout 

我我完全爲此感到困惑,曾嘗試使用include和include_once代碼,但仍然沒有改變。

+0

「redeclare問題」意味着您多次加載班級。 我推薦你使用'composer',它爲你處理所有的自動加載。而且,Laravel默認支持它。你遇到過這個問題嗎? 重現錯誤的最小代碼是什麼? – 2015-04-02 14:12:09

+0

我確實使用作曲家,但我仍然遇到這個問題 – user3718908 2015-04-02 15:00:27

+0

最小編碼?爲了重現錯誤,您需要庫本身來嘗試幷包含在您的測試應用程序中。 – user3718908 2015-04-02 15:11:06

回答

0
1.Add the mpower composer package to your composer.json file instead of adding the library manually 

    "require": { 
     "laravel/framework": "5.2.*", 
     "sirakoff/mpower_php":"dev-master" 
    }, 

2.Autoload the package by adding this to your composer.json file 

    "psr-0": { 
     "Sirakoff\\":["src/"] 
    } 

3. Set Mpower keys and Tokens in your controllers constructor method 

    public function __construct(){ 
     \MPower_Setup::setMasterKey("dd6f2c90-f075-012f-5b69-00155d866600"); 
     \MPower_Setup::setPublicKey("test_public_oDLVlm1eNyh0IsetdhdJvcl0ygA"); 
     \MPower_Setup::setPrivateKey("test_private_zzF3ywvX9DE-OSDNhUqKoaTI4wc"); 
     \MPower_Setup::setMode("test"); 
     \MPower_Setup::setToken("ca03737cf942cf644f36"); 

    } 


4. Now you can make use of the package in your controller 
    public function makePayment(Request $request) 
{ 
     $co = new \MPower_Checkout_Invoice(); 

     //addItem(name_of_item,quantity,unit_price,total_price,optional_description) 
     $co->addItem("13' Apple Retina 500 HDD",1,1.99,1.99); 
     $co->addItem("Case Logic laptop Bag",2,1.50,3.00,"Black Color with white stripes"); 
     $co->addItem("Mordecai's Bag",2,1.99,3.98); 

     $co->setTotalAmount(8.97); 

     $co->setDescription("Payment for general goods."); 

     $co->addTax("VAT (15)",50); 
     $co->addTax("NHIL (10)",50) 

     $co->addCustomData("Firstname","Alfred"); 
     $co->addCustomData("Lastname","Rowe"); 
     $co->addCustomData("CartId",929292872); 

     if($co->create()) { 
      //Your code here 
     } 


} 
+2

請爲OP添加一些解釋,以瞭解爲什麼他/她的代碼不能正常工作,以及這將如何幫助他/她,因此他/她可以從這個答案中學習。 – davejal 2016-05-15 00:44:49

+0

雖然這段代碼可能回答這個問題,但最好包含一些上下文,解釋它的工作原理以及何時使用它。從長遠來看,僅有代碼的答案是沒有用的。 – Bono 2016-05-15 07:08:57

+0

感謝您的提醒。 – 2016-05-15 09:38:57