PHP使类v7兼容

时间:2017-05-19 17:41:26

标签: php class compatibility

我有一个旧的类文件,因为将我的PHP版本更新为7现在报告错误。

"与其班级同名的方法不会 未来版本的PHP"

中的构造函数

我已阅读了几本指南,并尝试了所建议的内容:

https://cweiske.de/tagebuch/php4-constructors-php7.htm

目前我对该课程的编辑修改如下:

class tFPDF {

     public function __construct() {

     }

     var $unifontSubset;
     var $page;               // current page number
     ....
     var $PDFVersion;         // PDF version number

     function tFPDF($orientation='P', $unit='mm', $size='A4') {

          $this->StdPageSizes = array('a4'=>array(595.28,841.89));

     }

     function _getpagesize($size) {

          $size = strtolower($size);
          if(!isset($this->StdPageSizes[$size]))
               $this->Error('Unknown page size: '.$size);
          }

     }

     function AddPage($orientation='', $size=''){

          $size = $this->_getpagesize($size);

     }

}

然后在我的代码库中调用以下内容:

$ pdf-> AddPage(' P',' A4');

这会引发错误:未知页面大小:a4

因此,似乎设置$ this-> StdPageSizes属性的初始类未运行或正在被读取。这在旧版本的PHP中运行良好,所以我猜我错过了一个基本步骤。

我还看了一下该类的更新版本,可能与php&但它似乎不再受支持了。

任何人都可以帮助我使我的课程兼容,谢谢!

2 个答案:

答案 0 :(得分:2)

您的旧式构造函数签名与__construct签名不匹配,因此当您的代码调用构造函数时,$this->StdPageSizes数组不会被初始化。

您可以通过将旧的构造函数代码移动到__construct方法来解决此问题:

public function __construct($orientation = 'P', $unit = 'mm', $size = 'A4') 
{
    $this->StdPageSizes = array('a4' => array(595.28, 841.89));
}

为了避免重复构造函数代码,可以从旧构造函数中调用__construct方法:

public function tFPDF($orientation = 'P', $unit = 'mm', $size = 'A4') 
{
    self::__construct($orientation, $unit, $size);
}

答案 1 :(得分:1)

只有建筑师失踪。

但是,创建一个扩展旧概念的新对象对未来更为重要。例如,如果更新将遵循旧的东西。 :)

class tFPDFv7 
    extends tFPDF 
{
    public function __construct($orientation='P', $unit='mm', $size='A4') {
        $this->tFPDF($orientation, $unit, $size);
    }
}