Search code examples
phpmethodsundefinedoscommerce

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


I have been given the task of migrating an OSCommerce website from a server running PHP 5.3.3 to a server running PHP 7.0.33. The current live website uses OSCommerce version 2.3.4 and I know from reading on the OSC forums that this will cause a lot of errors when moving to a more recent PHP server.

Ideally I should upgrade OSC to the latest stable release but there is a lot of custom code that would potentially be an absolute nightmare to integrate and besides I'm under a time constraint to get the site moved and up and running again.

I have cleared a lot of basic errors and warnings but the one error I'm having trouble with is the call to the 'messageStack' class which displays a ajax/jquery information box to show items added to the cart or issues with forms etc.

The error is:

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

The line in question is:

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

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

Does anyone have an idea as to why this is happening? I've posted this exact question on the OSCommerce forums but as yet no reply so I thought I would ask here in case any of you kind people could possibly help.


Solution

  • Until PHP 7.0, you could define the constructor of a class by writing a function that has the exact same name as the class, which seems to be the case here : extends alertBlock and $this->alertBlock().

    Now, this behaviour is deprecated : 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.

    You can modify your $this->alertBlock(args) with parent::__construct(args)