2012-08-17 32 views
1

所以我有兩個文件,「header.php文件」和「pluginfile.php」「調用未定義功能」 - 試圖從包含的文件

,我想打電話給駐留在「函數調用函數pluginfile .PHP」是:

public function getNonSubscriptionAmount() { 
    $total = 0; 
    foreach($this->_items as $item) { 
     if(!$item->isSubscription()) { 
     $total += $item->getProductPrice() * $item->getQuantity(); 
     } 
     else { 
     // item is subscription 
     $basePrice = $item->getBaseProductPrice(); 
     Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Item is a subscription with base price $basePrice"); 
     $total += $basePrice; 
     } 
    } 
    return $total; 
    } 

所以 '的header.php' 我有:

<?php 
include_once($_SERVER['DOCUMENT_ROOT']."/wp-content/plugins/plugin-name/folder/PluginFile.php"); 
print getNonSubscriptionAmount(); 
?> 

這使得當任何頁面加載以下錯誤:

Fatal error: Call to undefined function getnonsubscriptionamount() in /home/username/domain.com/wp-content/themes/theme/header.php on line 72

我已經花了幾個小時,現在想單獨算出這個和我一事無成!任何幫助非常感謝!

+1

你可以試着改變你的include_once到require_once可確保文件被正確地包含在的情況下也有一些是與尋路怎麼回事。 – SidewaysGravity 2012-08-17 17:55:32

+4

'公共函數...'這告訴我它被定義爲一個類方法,但是你把它稱爲獨立函數。 – Wiseguy 2012-08-17 17:55:36

+0

好抓@Wiseguy,那肯定會造成問題。 – SidewaysGravity 2012-08-17 17:56:49

回答

1

@Wiseguy看起來他當時的想法是放在評論。

你聲明的方法,而不是一個功能。該函數是plugin.php文件的整體還是更多?如果是一切,刪除公共修飾符,只聲明

function getNonSubscriptionAmount() { 
    // code here 
} 

但是從代碼的外觀來看,它是更大類的一部分。如果是這種情況,那麼@Wiseguy註釋是正確的,你需要在plugin.php中實例化一個新類的對象,然後實現所需的方法。

$obj = new PluginClass(); 
$obj->getNonSubscriptionAmount(); 
+0

我仍然不確定我把代碼放在哪裏,我只是得到錯誤! – Jambo 2012-08-17 19:44:18

0

你說:

The function that I want to call resides in 'plugin.php' and is:

而在你的文件要包括:

So in 'header.php' I have: include_once($_SERVER['DOCUMENT_ROOT']."/wp-content/plugins/plugin-name/folder/PluginFile.php"); print getNonSubscriptionAmount();

您還沒有包括 'plugin.php' 這是功能的生活。

header.php中應該包括 'plugin.php',而不是 'PluginFile.php'。

+0

道歉,這是我寫這個問題的錯誤! – Jambo 2012-08-17 18:11:46

+0

單詞,在這種情況下,我的回答是沒有幫助的,呵呵。 – 2012-08-17 18:18:41

相關問題