【问题标题】:How to fix 'Uncaught Error: Call to undefined method' in PHP如何在 PHP 中修复“未捕获的错误:调用未定义的方法”
【发布时间】:2019-05-08 09:41:22
【问题描述】:

我的任务是将 OSCommerce 网站从运行 PHP 5.3.3 的服务器迁移到运行 PHP 7.0.33 的服务器。当前的实时网站使用 OSCommerce 版本 2.3.4,我从 OSC 论坛上的阅读中知道,当迁移到更新的 PHP 服务器时,这会导致很多错误。

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

我已经清除了许多基本错误和警告,但我遇到的一个错误是调用“messageStack”类,该类显示 ajax/jquery 信息框以显示添加到购物车的项目或问题表格等

错误是:

致命错误:未捕获的错误:调用未定义的方法 messageStack::alertBlock() 在 /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): include('/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 论坛上发布了这个确切的问题,但还没有回复,所以我想我会在这里问一下,以防各位好心人可以提供帮助。

【问题讨论】:

  • 没有名为 alertBlock 的方法。有什么问题?
  • 等等,你是想给整个班级打电话alertBlock?也许你想要new alertBlock 代替?
  • 很抱歉,我在类和对象方面的 php 技能恐怕不是那么好。你能解释一下关于使用新的 alertBlock 的更多信息吗?
  • 只需创建一个alertBlock 类的实例。您在该类中的代码将自动运行

标签: php methods undefined oscommerce


【解决方案1】:

在 PHP 7.0 之前,您可以通过编写与类具有完全相同名称的函数来定义类的构造函数,这里似乎就是这种情况:extends alertBlock$this-&gt;alertBlock()

现在,这种行为已被弃用:PHP constructors

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

您可以使用parent::__construct(args) 修改您的$this-&gt;alertBlock(args)

【讨论】:

    【解决方案2】:

    您必须在“alertBox”类中创建与该类不同名称的方法,然后才能轻松访问该类的方法!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-22
      • 1970-01-01
      • 1970-01-01
      • 2019-09-03
      • 1970-01-01
      • 1970-01-01
      • 2021-06-05
      • 1970-01-01
      相关资源
      最近更新 更多