变量范围从php5 / smarty2.6迁移到php7 / smarty3.1

时间:2017-03-14 00:49:11

标签: php-7 smarty3

我目前正尝试将PHP5和Smarty 2.6中的一些网站升级到PHP7和Smarty 3.1.31。

我们以前有一个扩展其他类的类系统的站点,以下是它的简化版本:

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正确的网站。我会直接使用$ 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?我是否必须为每个变量做一个或者通用工作? (似乎聪明的已经有一个不起作用的通用)

1 个答案:

答案 0 :(得分: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;
    }
}
相关问题