【问题标题】:variable scope migrating from php5/smarty2.6 to php7/smarty3.1变量范围从 php5/smarty2.6 迁移到 php7/smarty3.1
【发布时间】:2017-08-04 04:48:40
【问题描述】:

我目前正在尝试将一些使用 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 行的函数,它是通用的 setter:

/**
* <<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 中发生了一些变化,这是否告诉我我需要为站点类中的所有变量设置设置器?我必须为每个变量做一个还是通用的工作? (似乎 smarty 已经有一个不起作用的泛型)

【问题讨论】:

  • Smarty 的类如何使用?您从 Smarty 库中调用了哪些函数以及如何调用?

标签: php-7 smarty3


【解决方案1】:
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;
    }
}

【讨论】:

  • 我会试试这个。这是否只是 php5 非常宽容并代表我们的应用程序声明变量(如果它以前没有找到它们)的一个功能,而 php7 则希望应用程序非常明确?
  • 我不这么认为,您可以通过在 php.ini 文件中将 display_errors 设置为 no 来隐藏此通知
猜你喜欢
  • 2021-01-28
  • 2020-08-07
  • 2020-03-09
  • 1970-01-01
  • 2019-07-05
  • 2021-05-15
  • 1970-01-01
  • 2019-03-09
  • 2021-01-03
相关资源
最近更新 更多