How to fix 'Uncaught Error: Call to undefined method' in PHP

How to fix 'Uncaught Error: Call to undefined method' in PHP

我的任务是将 OSCommerce 网站从服务器 运行 PHP 5.3.3 迁移到服务器 运行 PHP 7.0.33。当前的在线网站使用 OSCommerce 2.3.4 版,我从 OSC 论坛上了解到,当移动到更新的 PHP 服务器时,这会导致很多错误。

理想情况下,我应该将 OSC 升级到最新的稳定版本,但是有很多自定义代码可能会成为集成的绝对噩梦,而且我在时间限制下移动和更新网站 运行 再一次。

我已经清除了很多基本错误和警告,但我遇到的一个错误是调用 'messageStack' class,它显示 ajax/jquery 信息框显示添加到购物车的商品或表格问题等。

错误是:

Fatal error: Uncaught Error: Call to undefined method messageStack::alertBlock() in /home/xxxx/public_html/xxxx/includes/classes/message_stack.php:66 Stack trace: #0 /home/xxxx/public_html/xxxx/includes/modules/product_listing.php(4): messageStack->output('product_action') #1 /home/xxxx/public_html/xxxx/index.php(227): include('/home/xxxx...') #2 {main} thrown in /home/xxxx/public_html/xxxx/includes/classes/message_stack.php on line 66

有问题的行是:

return $this->alertBlock($output);

message_stack.php

 class messageStack extends alertBlock {

// class constructor
    function __construct() {
      $this->messages = array();

      if (isset($_SESSION['messageToStack'])) {
        for ($i=0, $n=sizeof($_SESSION['messageToStack']); $i<$n; $i++) {
          $this->add($_SESSION['messageToStack'][$i]['class'], $_SESSION['messageToStack'][$i]['text'], $_SESSION['messageToStack'][$i]['type']);
        }
        unset($_SESSION['messageToStack']);
      }
    }

// class methods
    function add($class, $message, $type = 'error') {
      if ($type == 'error') {
        $this->messages[] = array('params' => 'class="alert alert-danger alert-dismissible"', 'class' => $class, 'text' => $message);
      } elseif ($type == 'warning') {
        $this->messages[] = array('params' => 'class="alert alert-warning alert-dismissible"', 'class' => $class, 'text' => $message);
      } elseif ($type == 'success') {
        $this->messages[] = array('params' => 'class="alert alert-success alert-dismissible"', 'class' => $class, 'text' => $message);
      } else {
        $this->messages[] = array('params' => 'class="alert alert-info alert-dismissible"', 'class' => $class, 'text' => $message);
      }
    }

    function add_session($class, $message, $type = 'error') {
      if (!isset($_SESSION['messageToStack'])) {
        $_SESSION['messageToStack'] = array();
      }

      $_SESSION['messageToStack'][] = array('class' => $class, 'text' => $message, 'type' => $type);
    }

    function reset() {
      $this->messages = array();
    }

    function output($class) {
      $output = array();
      for ($i=0, $n=sizeof($this->messages); $i<$n; $i++) {
        if ($this->messages[$i]['class'] == $class) {
          $output[] = $this->messages[$i];
        }
      }

      return $this->alertBlock($output);
    }

    function size($class) {
      $count = 0;

      for ($i=0, $n=sizeof($this->messages); $i<$n; $i++) {
        if ($this->messages[$i]['class'] == $class) {
          $count++;
        }
      }

      return $count;
    }
  }

和alertbox.php

class alertBlock {    
    // class constructor
    function __construct($contents, $alert_output = false) {
      $alertBox_string = '';

      for ($i=0, $n=sizeof($contents); $i<$n; $i++) {
        $alertBox_string .= '  <div';

        if (isset($contents[$i]['params']) && tep_not_null($contents[$i]['params']))
          $alertBox_string .= ' ' . $contents[$i]['params'];

          $alertBox_string .= '>' . "\n";
          $alertBox_string .= ' <button type="button" class="close" data-dismiss="alert">&times;</button>' . "\n";
          $alertBox_string .= $contents[$i]['text'];

          $alertBox_string .= '  </div>' . "\n";
      }

      if ($alert_output == true) echo $alertBox_string;
        return $alertBox_string;
     }
  }

有人知道为什么会这样吗?我已经在 OSCommerce 论坛上发布了这个确切的问题,但目前还没有回复,所以我想我会在这里问一下,以防你们中的任何人可能提供帮助。

您必须创建与 class 不同名称的方法到 "alertBox" class 然后您可以轻松访问此 class 的方法!

直到 PHP 7.0,您可以通过编写一个与 class 具有完全相同名称的函数来定义 class 的构造函数,这里似乎就是这种情况: extends alertBlock$this->alertBlock()

现在,此行为已弃用:PHP constructors

Old style constructors are DEPRECATED in PHP 7.0, and will be removed in a future version. You should always use __construct() in new code.

您可以将 $this->alertBlock(args) 修改为 parent::__construct(args)