2017-03-14 51 views
0

我目前正在嘗試將我們的幾個PHP5和Smarty 2.6網站升級到PHP7和Smarty 3.1.31。從php5/smarty2.6遷移到php7/smarty3.1的可變範圍

我們以前有一個網站與延伸其他類的類的系統,這裏是它的一個簡化版本:

class Site extends WebView 
{ 
    //functions 
} 

class WebView extends LandingPage 
{ 
    //functions 
} 

class LandingPage extends Smarty 
{ 
    function LandingPage() 
    { 
     $this->sessionInit = false; 

     $this->flag = array(); 
     $this->engineVersion = ''; 

     $this->charset = 'utf-8'; 
     $this->content_type = 'text/html'; 
     $this->strings = array(); 
     $this->rtid=null; 
    } 
} 

之前,這是工作的罰款所有的變量都是正確的網站使用$this->variable_name 。我oculd訪問他們直接使用$ this,如$this->flag['TEST']從內部類,或外部我可以使用$site,如$site->flag['TEST']。但是現在我得到了一系列未定義的屬性錯誤

Notice: Undefined property: Site::$sessionInit in /var/www/html/vendor/smarty/smarty/libs/Smarty.class.php on line 1447 
Notice: Undefined property: Site::$flag in /var/www/html/vendor/smarty/smarty/libs/Smarty.class.php on line 1447 
Notice: Undefined property: Site::$engineVersion in /var/www/html/vendor/smarty/smarty/libs/Smarty.class.php on line 1447 
Notice: Undefined property: Site::$charset in /var/www/html/vendor/smarty/smarty/libs/Smarty.class.php on line 1447 
Notice: Undefined property: Site::$content_type in /var/www/html/vendor/smarty/smarty/libs/Smarty.class.php on line 1447 
Notice: Undefined property: Site::$strings in /var/www/html/vendor/smarty/smarty/libs/Smarty.class.php on line 1447 
Notice: Undefined property: Site::$rtid in /var/www/html/vendor/smarty/smarty/libs/Smarty.class.php on line 1447 

我走進了Smarty類(很明顯,我沒有寫這一點),並搶下了線路1447的功能,這是一般的二傳手:

/** 
* <<magic>> Generic setter. 
* Calls the appropriate setter function. 
* Issues an E_USER_NOTICE if no valid setter is found. 
* 
* @param string $name property name 
* @param mixed $value parameter passed to setter 
*/ 
public function __set($name, $value) 
{ 
    if (isset($this->accessMap[ $name ])) { 
     $method = 'set' . $this->accessMap[ $name ]; 
     $this->{$method}($value); 
    } elseif (in_array($name, $this->obsoleteProperties)) { 
     return; 
    } else { 
     if (is_object($value) && method_exists($value, $name)) { 
      $this->$name = $value; 
     } else { 
      trigger_error('Undefined property: ' . get_class($this) . '::$' . $name, E_USER_NOTICE); 
     } 
    } 
} 

它是通用的,並且在任何地方使用,所以我會認爲這很好,問題是我如何調用它。我得到的是錯誤如何引用Site::$sessionInit,好像該變量必須是網站中的網站,並且當我嘗試將其設置在目標網頁中時會變得生氣。

我知道變量作用域已經改變了一些在php7中,這是否告訴我,我需要setter的所有我的變量在網站類?我需要爲每個變量做一個還是一個通用的工作? (看起來像smarty已經有一個通用的不工作)

+0

如何與Smarty的使用類?你從Smarty圖書館撥打什麼功能? – m13r

回答

0
Notice: Undefined property: Site::$sessionInit in /var/www/html/vendor .... 
.... 
.... 

這種類型的錯誤並不意味着變量是從它的作用域中調用的。

這意味着你正試圖調用一個前面沒有定義的變量。

,並解決這個問題,你只需要定義這些變量如下:

class LandingPage extends Smarty 
{ 
    public $sessionInit; 
    public $flag; // or what ever the right visibility term 
    // .... and so on 
    function LandingPage() 
    { 
     $this->sessionInit = false; 

     $this->flag = array(); 
     $this->engineVersion = ''; 

     $this->charset = 'utf-8'; 
     $this->content_type = 'text/html'; 
     $this->strings = array(); 
     $this->rtid=null; 
    } 
} 
+0

我會試試這個。這僅僅是php5的一個功能,如果它以前沒有找到它們,它將非常寬容並在我們的應用程序中聲明變量,而php7希望應用程序非常明確? – Josh

+0

我不這麼認爲,你可以通過在php.ini文件中將'display_errors'設置爲no來隱藏這個通知 – hassan