【发布时间】: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 库中调用了哪些函数以及如何调用?