手上有幾個舊的網站用 TemplatePower 作為樣版,早期相安無事,現在 PHP 升到 PHP 7 以後,就看到系統會不停的噴出類似這樣的 error log:

PHP Deprecated:  Methods with the same name as their class will not be constructors in a future version of PHP; TemplatePower has a deprecated constructor in D:\templatepower\class.TemplatePower.inc.php on line 499

雖然頁面還是可以照樣正常套版,但是在 debug 時一直看到這些 log 交錯其中,覺得神煩,參考了 stackoverflow "PHP7 Constructor class name",原來以前舊版的 PHP 的物件導向中,類別 (class) 的建構方法 (constructor) 可以同名,現在必須要統一使用建構子 (__construct)。

所以依照 PHP 的新規則稍作修改,主要是加個 function __construct() { } 來做本來物件初始化時要做的事,像是從這樣(大約在第 60 行)

<?php
// 原有程式
function
TemplatePowerParser( $tpl_file, $type )
   {
       $this->version = '3.0.2';

       $this->tpl_base = Array( $tpl_file, $type );
       $this->tpl_count = 0;
         $this->ignore_stack = Array( false );
   }
?> 

改成這樣:

<?php
// 修改後
function
__construct($tpl_file, $type)
   {
       $this->version = '3.0.2';
       $this->tpl_base = Array( $tpl_file, $type );
       $this->tpl_count = 0;
       $this->ignore_stack = Array( false );
   }

   function TemplatePowerParser( $tpl_file, $type )
   {
       self::__construct($tpl_file, $type);
   }
?>


底下還有一段(大約在第 467 行)

<?php
// 原有程式
function
TemplatePower( $tpl_file='', $type= T_BYFILE )
    {
        TemplatePowerParser::TemplatePowerParser( $tpl_file, $type );

        $this->prepared = false;
        $this->showUnAssigned = false;
            $this->serialized = false; //added: 26 April 2002
    }
?>  

也要跟著改:

<?php
// 修改後
function
__construct($tpl_file='', $type= T_BYFILE){
        TemplatePowerParser::TemplatePowerParser( $tpl_file, $type );
        $this->prepared = false;
        $this->showUnAssigned = false;
        $this->serialized = false; //added: 26 April 2002
    }
    function TemplatePower( $tpl_file='', $type= T_BYFILE )
    {
        self::__construct($tpl_file, $type);
    }
?>

 

改好以後 log 就乾淨多了。打完收工。

 

arrow
arrow
    文章標籤
    php templatepower
    全站熱搜

    小攻城師 發表在 痞客邦 留言(1) 人氣()