【发布时间】:2017-10-24 20:25:31
【问题描述】:
我遇到的 PHP 问题可能是我遇到过的最令人困惑的问题。请看以下代码:
public $profile;
public $account;
function __construct(){
if(isset($_SESSION['uid'])){
$this->$profile = $_SESSION['user_profile'];
$this->$account = $_SESSION['user_account'];
echo "<script> alert('".$this->$profile->forename."'); </script>"; //Shows nothing
}else{
unset($_SESSION['user_profile']);
unset($_SESSION['user_account']);
}
}
由于某种原因,似乎要么写$this->$profile,要么写$this->$account,但前提是它是最后一个。在上述情况下,如果我将配置文件行移动到帐户行之后,它将被写入。但是,在这种情况下它不是。
$_SESSION 两个变量都是从 SQL 语句中检索到的对象,它们的赋值是有效的,因为直接访问其中一个变量(例如 $_SESSION['user_profile']->forename)工作正常。
有什么想法吗?谢谢。
【问题讨论】:
-
尝试使用
$this->profile和$this->account代替$this->$profile、$this->$account -
很好看的@RyanHipkiss
-
@RyanHipkiss 这似乎奏效了。你知道为什么吗?我想我以前没有遇到过这样的麻烦。感谢您的评论。
-
加上额外的美元符号,它们是
variable variables,所以在这两种情况下它们可能会转到$this->null并相互覆盖。
标签: php variables variable-assignment