如何在 PHP 中修复'Uncaught Error: Call to undefined method'



我已被赋予了从运行php 5.3.3的服务器迁移到运行PHP 7.0.33的服务器的任务。当前的实时网站使用Oscommerce版本2.3.4,我从OSC论坛上的阅读中知道,这将在移至最新的PHP服务器时会导致很多错误。

理想情况下,我应该将OSC升级到最新的稳定版本,但是有很多自定义代码可能绝对是一场噩梦,而且我在时间限制下以使网站移动并启动并运行。

我已经清除了许多基本错误和警告,但是我遇到的一个错误是呼吁" Messagestack"类的呼吁,该类显示一个Ajax/jQuery信息框,以显示添加到购物车中的项目或与表格等

错误是:

致命错误:未经定义的方法致电:未定义的方法 Messagestack :: AlertBlock((in /home/xxxx/public_html/xxxx/includes/classes/message_stack.php:66 堆栈跟踪:#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(: 包括('/home/xxxx ...'(#2 {main}扔进 /home/xxxx/public_html/xxxx/includes/classes/message_stack.php 在第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论坛上发布了这个确切的问题,但是尚无答复,所以我想在这里我会在这里问,以防你们中的任何人可能会有所帮助。

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

现在,这种行为被弃用:PHP构造函数

旧样式的构造函数已在PHP 7.0中弃用,并将在将来的版本中删除。您应始终在新代码中使用__construct((。

您可以使用parent::__construct(args)

修改$this->alertBlock(args)

您必须创建从类别中使用不同名称的方法到" arterbox"类中,然后在您可以轻松访问此类方法之后!

相关内容

最新更新