dongzha5934
dongzha5934
2012-08-17 17:53
浏览 137
已采纳

'调用未定义的函数' - 尝试从包含的文件调用函数

So I have two files, 'header.php' and 'pluginfile.php'

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

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;
  }

So in 'header.php' I have:

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

This gives the following error when any page is loaded:

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

I've spent a couple of hours now trying to figure this out alone and am getting nowhere! Any help much appreciated!

图片转代码服务由CSDN问答提供 功能建议

所以我有两个文件,'header.php'和'pluginfile.php' \ n

我要调用的函数位于'pluginfile.php'中,并且是:

  public function getNonSubscriptionAmount(){
 $ total = 0; 
  foreach($ this-&gt; _items as $ item){
 if(!$ item-&gt; isSubscription()){
 $ total + = $ item-&gt; getProductPrice()* $ item-&gt; getQuantity(  }; 
} 
其他{
 // item is subscription 
 $ basePrice = $ item-&gt; getBaseProductPrice(); 
 Cart66Common :: log('['。basename(__ FILE__)。' -  line'  .__LINE__。“]项目是一个基本价格为$ basePrice的订阅”); 
 $ total + = $ basePrice; 
} 
} 
返回$ total; 
} 
   
 
 

所以在'header.php'中我有:

 &lt;?php 
include_once($ _ SERVER ['DOCUMENT_ROOT']。“/ wp  -content / plugins / plugin-name / folder / PluginFile.php“); 
print getNonSubscriptionAmount(); 
?&gt; 
   
 
 

这给出了以下e 加载任何页面时出错:

致命错误:在 /home/username/domain.com/wp-content/themes中调用未定义函数getnonsubscriptionamount() /theme/header.php在 第72行

我现在花了几个小时试图独自解决这个问题并且无处可去! 任何帮助非常感谢!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongliang1893
    dongliang1893 2012-08-17 18:04
    已采纳

    @Wiseguy looks like he had the right idea put in the comments.

    You are declaring a method and not a function. Is the function the entirety of your plugin.php file or is there more? If it is everything, remove the public modifier and just declare

    function getNonSubscriptionAmount() {
      // code here
    }
    

    But from the looks of the code it is part of a larger class. If thats the case then @Wiseguy comment is right on, you need to instantiate a new object of the class in plugin.php and then the desired method.

    $obj = new PluginClass();
    $obj->getNonSubscriptionAmount();
    
    点赞 评论
  • doyp9057
    doyp9057 2012-08-17 18:10

    You said:

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

    And in your file you are including:

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

    You are not including 'plugin.php' which is were the function lives.

    Header.php should include 'plugin.php', not 'PluginFile.php'.

    点赞 评论

相关推荐