我不太清楚为什么会发生这种情况,或者如何正确地解释它,但也许有人可以对此进行说明。变量为空直到重新分配
我有一个基于CodeIgniter/Opencart框架利用注册表,控制器和模块的CMS系统。我遇到了,我以前保存的变量到注册表的情景:
$this->application_page = 'current/page';
但由于某些原因,当我把它叫做中的应用:
echo empty($this->application_page)?'yes':'no';
//Returns Yes
但是..当我重新分配它:
echo empty($this->application_page)?'yes':'no';
//Returns Yes
$page = $this->application_page;
echo empty($page)?'yes':'no';
//Returns No
一个的var_dump回报:
var_dump($this->application_page);
string 'current/page' (length=12)
只需使用$page
即可轻松解决此问题,但我很想知道为什么会发生这种情况?
UPDATE:
所以我用_isset
功能混乱周围,但没有得到它的工作,可能是我的错误,可能不是。这里是这一切是如何一起工作的:
class Registry {
private $data = array();
public function get($key){ return (isset($this->data[$key]) ? $this->data[$key] : NULL); }
public function set($key,$val){ $this->data[$key] = $val;
}
abstract class Controller {
protected $registry;
public function __construct($registry){ $this->registry = $registry; }
public function __get($key){ return $this->registry->get($key); }
public function __set($key,$value){ $this->registry->set($key, $value); }
}
class Applications {
private $registry;
function __construct($Registry){ $this->registry = $Registry; }
function __get($key){ return $this->registry->get($key); }
function __set($key,$val){ return $this->registry->set($key,$val); }
public function buildApplication(){
$this->application_page = 'current/page';
$application = new application($this->registry);
}
}
class Application extends Controller {
public function index(){
echo empty($this->application_page)?'yes':'no';
//Returns Yes
$page = $this->application_page;
echo empty($page)?'yes':'no';
//Returns No
}
}
希望这有助于? 有一个错字,注册表的功能不是神奇的方法。在应用程序中还声明了$ registry。
你是否100%确定你没有在你的var名错误,而测试呢? – NDM
对不起延迟!是的,我确定,我在发帖前加倍检查了大约5次=) – Corey
@Corey请看我更新的答案。 – ComFreek