drvkf88226 2016-02-01 10:34
浏览 33
已采纳

PHP扩展类

I am rewriting some of my code for PHP 7 compatibility. While most classes I rewrote work fine, I am having an issue with the extended classes that try to access functions from the original(parent) class, and wondering what I am doing wrong.

This is the main class:

  class tableBlock {
    var $table_border = '0';
    var $table_width = '100%';
    var $table_cellspacing = '0';
    var $table_cellpadding = '2';
    var $table_parameters = '';
    var $table_row_parameters = '';
    var $table_data_parameters = '';

//function tableBlock($contents) { // modified for php 7 compatibility
    function __construct($contents) {
      $tableBox_string = '';

      $form_set = false;
      if (isset($contents['form'])) {
        $tableBox_string .= $contents['form'] . "
";
        $form_set = true;
        array_shift($contents);
      }

      $tableBox_string .= '<table border="' . $this->table_border . '" width="' . $this->table_width . '" cellspacing="' . $this->table_cellspacing . '" cellpadding="' . $this->table_cellpadding . '"';
      if (tep_not_null($this->table_parameters)) $tableBox_string .= ' ' . $this->table_parameters;
      $tableBox_string .= '>' . "
";

      for ($i=0, $n=sizeof($contents); $i<$n; $i++) {
        $tableBox_string .= '  <tr';
        if (tep_not_null($this->table_row_parameters)) $tableBox_string .= ' ' . $this->table_row_parameters;
        if (isset($contents[$i]['params']) && tep_not_null($contents[$i]['params'])) $tableBox_string .= ' ' . $contents[$i]['params'];
        $tableBox_string .= '>' . "
";

        if (isset($contents[$i][0]) && is_array($contents[$i][0])) {
          for ($x=0, $y=sizeof($contents[$i]); $x<$y; $x++) {
            if (isset($contents[$i][$x]['text']) && tep_not_null($contents[$i][$x]['text'])) {
              $tableBox_string .= '    <td';
              if (isset($contents[$i][$x]['align']) && tep_not_null($contents[$i][$x]['align'])) $tableBox_string .= ' align="' . $contents[$i][$x]['align'] . '"';
              if (isset($contents[$i][$x]['params']) && tep_not_null($contents[$i][$x]['params'])) {
                $tableBox_string .= ' ' . $contents[$i][$x]['params'];
              } elseif (tep_not_null($this->table_data_parameters)) {
                $tableBox_string .= ' ' . $this->table_data_parameters;
              }
              $tableBox_string .= '>';
              if (isset($contents[$i][$x]['form']) && tep_not_null($contents[$i][$x]['form'])) $tableBox_string .= $contents[$i][$x]['form'];
              $tableBox_string .= $contents[$i][$x]['text'];
              if (isset($contents[$i][$x]['form']) && tep_not_null($contents[$i][$x]['form'])) $tableBox_string .= '</form>';
              $tableBox_string .= '</td>' . "
";
            }
          }
        } else {
          $tableBox_string .= '    <td';
          if (isset($contents[$i]['align']) && tep_not_null($contents[$i]['align'])) $tableBox_string .= ' align="' . $contents[$i]['align'] . '"';
          if (isset($contents[$i]['params']) && tep_not_null($contents[$i]['params'])) {
            $tableBox_string .= ' ' . $contents[$i]['params'];
          } elseif (tep_not_null($this->table_data_parameters)) {
            $tableBox_string .= ' ' . $this->table_data_parameters;
          }
          $tableBox_string .= '>' . $contents[$i]['text'] . '</td>' . "
";
        }

        $tableBox_string .= '  </tr>' . "
";
      }

      $tableBox_string .= '</table>' . "
";

      if ($form_set == true) $tableBox_string .= '</form>' . "
";

      return $tableBox_string;
    }
  }

This is the extended class:

class box extends tableBlock {
//    function box() {  // modified for php 7 compatibility
    function __construct() {
      $this->heading = array();
      $this->contents = array();
    }


function menuBox($heading, $contents) {

    global $menu_dhtml;              // add for dhtml_menu
    if ($menu_dhtml == false ) {     // add for dhtml_menu

      $this->table_data_parameters = 'class="menuBoxHeading"';
      if ($heading[0]['link']) {
        $this->table_data_parameters .= ' onmouseover="this.style.cursor=\'hand\'" onclick="document.location.href=\'' . $heading[0]['link'] . '\'"';
        $heading[0]['text'] = '&nbsp;<a href="' . $heading[0]['link'] . '" class="menuBoxHeadingLink">' . $heading[0]['text'] . '</a>&nbsp;';
      } else {
        $heading[0]['text'] = '&nbsp;' . $heading[0]['text'] . '&nbsp;';
      }
      $this->heading = $this->tableBlock($heading);

      $this->table_data_parameters = 'class="menuBoxContent"';
      $this->contents = $this->tableBlock($contents);
      return $this->heading . $this->contents . $dhtml_contents;
// ## add for dhtml_menu
    } else {
      $selected = substr(strrchr ($heading[0]['link'], '='), 1);
      $dhtml_contents = $contents[0]['text'];
      $change_style = array ('<br>'=>' ','<BR>'=>' ', 'a href='=> 'a class="menuItem" href=','class="menuBoxContentLink"'=>' ');
      $dhtml_contents = strtr($dhtml_contents,$change_style);
      $dhtml_contents = '<div id="'.$selected.'Menu" class="menu" onmouseover="menuMouseover(event)">'. $dhtml_contents . '</div>';
      return $dhtml_contents;
      }
// ## eof add for dhtml_menu
    }
}

As you can see, I modified the constructors to be __construct, but the extended functions errors when it tries to access $this->contents = $this->tableBlock($heading); and $this->contents = $this->tableBlock($contents);

I tried to modify those lines, using $this->contents = parent::__construct($contents); and $this->contents = parent::__construct($heading); but I am probably writing this wrong as it doesn't work either.

Any help is greatly appreciated.

  • 写回答

2条回答 默认 最新

  • dongshi7433 2016-02-01 10:44
    关注

    The __construct function is meant to construct the object, which means it is there to create the object in memory and initialize some properties (if you need this). You have used this correctly in your extended class. However, you cannot return a value from the constructor: Returning a value in constructor function of a class

    I would recommend to rename your __construct function again to something like createTableBlock and call this function from your extended class with parent::createTableBlock($arguments).

    Also I would recommend to always call your parent constructor (if there is any). You can accomplish this by calling parent::__construct in the constructor of the extended class.

    As request by OP his code rewritten:

      class tableBlock {
        var $table_border = '0';
        var $table_width = '100%';
        var $table_cellspacing = '0';
        var $table_cellpadding = '2';
        var $table_parameters = '';
        var $table_row_parameters = '';
        var $table_data_parameters = '';
    
        function __construct() {
          //empty
        }
    
        function tableBlock($contents) {
          $tableBox_string = '';
    
          $form_set = false;
          if (isset($contents['form'])) {
            $tableBox_string .= $contents['form'] . "
    ";
            $form_set = true;
            array_shift($contents);
          }
    
          $tableBox_string .= '<table border="' . $this->table_border . '" width="' . $this->table_width . '" cellspacing="' . $this->table_cellspacing . '" cellpadding="' . $this->table_cellpadding . '"';
          if (tep_not_null($this->table_parameters)) $tableBox_string .= ' ' . $this->table_parameters;
          $tableBox_string .= '>' . "
    ";
    
          for ($i=0, $n=sizeof($contents); $i<$n; $i++) {
            $tableBox_string .= '  <tr';
            if (tep_not_null($this->table_row_parameters)) $tableBox_string .= ' ' . $this->table_row_parameters;
            if (isset($contents[$i]['params']) && tep_not_null($contents[$i]['params'])) $tableBox_string .= ' ' . $contents[$i]['params'];
            $tableBox_string .= '>' . "
    ";
    
            if (isset($contents[$i][0]) && is_array($contents[$i][0])) {
              for ($x=0, $y=sizeof($contents[$i]); $x<$y; $x++) {
                if (isset($contents[$i][$x]['text']) && tep_not_null($contents[$i][$x]['text'])) {
                  $tableBox_string .= '    <td';
                  if (isset($contents[$i][$x]['align']) && tep_not_null($contents[$i][$x]['align'])) $tableBox_string .= ' align="' . $contents[$i][$x]['align'] . '"';
                  if (isset($contents[$i][$x]['params']) && tep_not_null($contents[$i][$x]['params'])) {
                    $tableBox_string .= ' ' . $contents[$i][$x]['params'];
                  } elseif (tep_not_null($this->table_data_parameters)) {
                    $tableBox_string .= ' ' . $this->table_data_parameters;
                  }
                  $tableBox_string .= '>';
                  if (isset($contents[$i][$x]['form']) && tep_not_null($contents[$i][$x]['form'])) $tableBox_string .= $contents[$i][$x]['form'];
                  $tableBox_string .= $contents[$i][$x]['text'];
                  if (isset($contents[$i][$x]['form']) && tep_not_null($contents[$i][$x]['form'])) $tableBox_string .= '</form>';
                  $tableBox_string .= '</td>' . "
    ";
                }
              }
            } else {
              $tableBox_string .= '    <td';
              if (isset($contents[$i]['align']) && tep_not_null($contents[$i]['align'])) $tableBox_string .= ' align="' . $contents[$i]['align'] . '"';
              if (isset($contents[$i]['params']) && tep_not_null($contents[$i]['params'])) {
                $tableBox_string .= ' ' . $contents[$i]['params'];
              } elseif (tep_not_null($this->table_data_parameters)) {
                $tableBox_string .= ' ' . $this->table_data_parameters;
              }
              $tableBox_string .= '>' . $contents[$i]['text'] . '</td>' . "
    ";
            }
    
            $tableBox_string .= '  </tr>' . "
    ";
          }
    
          $tableBox_string .= '</table>' . "
    ";
    
          if ($form_set == true) $tableBox_string .= '</form>' . "
    ";
    
          return $tableBox_string;
        }
      }
    

    This is the extended class:

    class box extends tableBlock {
        function __construct() {
          parent::__construct(); //calling parent constructor
          $this->heading = array();
          $this->contents = array();
        }
    
    
    function menuBox($heading, $contents) {
        global $menu_dhtml;              // add for dhtml_menu
        if ($menu_dhtml == false ) {     // add for dhtml_menu
    
          $this->table_data_parameters = 'class="menuBoxHeading"';
          if ($heading[0]['link']) {
            $this->table_data_parameters .= ' onmouseover="this.style.cursor=\'hand\'" onclick="document.location.href=\'' . $heading[0]['link'] . '\'"';
            $heading[0]['text'] = '&nbsp;<a href="' . $heading[0]['link'] . '" class="menuBoxHeadingLink">' . $heading[0]['text'] . '</a>&nbsp;';
          } else {
            $heading[0]['text'] = '&nbsp;' . $heading[0]['text'] . '&nbsp;';
          }
          $this->heading = $this->tableBlock($heading);
    
          $this->table_data_parameters = 'class="menuBoxContent"';
          $this->contents = $this->tableBlock($contents);
          return $this->heading . $this->contents . $dhtml_contents;
    // ## add for dhtml_menu
        } else {
          $selected = substr(strrchr ($heading[0]['link'], '='), 1);
          $dhtml_contents = $contents[0]['text'];
          $change_style = array ('<br>'=>' ','<BR>'=>' ', 'a href='=> 'a class="menuItem" href=','class="menuBoxContentLink"'=>' ');
          $dhtml_contents = strtr($dhtml_contents,$change_style);
          $dhtml_contents = '<div id="'.$selected.'Menu" class="menu" onmouseover="menuMouseover(event)">'. $dhtml_contents . '</div>';
          return $dhtml_contents;
          }
    // ## eof add for dhtml_menu
        }
    }
    

    Now call the parent logic with $this->contents = parent::tableBlock($contents);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?