【问题标题】:Only the last variable in sequence being written仅写入序列中的最后一个变量
【发布时间】: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-&gt;$profile,要么写$this-&gt;$account,但前提是它是最后一个。在上述情况下,如果我将配置文件行移动到帐户行之后,它将被写入。但是,在这种情况下它不是。

$_SESSION 两个变量都是从 SQL 语句中检索到的对象,它们的赋值是有效的,因为直接访问其中一个变量(例如 $_SESSION['user_profile']-&gt;forename)工作正常。

有什么想法吗?谢谢。

【问题讨论】:

  • 尝试使用$this-&gt;profile$this-&gt;account代替$this-&gt;$profile$this-&gt;$account
  • 很好看的@RyanHipkiss
  • @RyanHipkiss 这似乎奏效了。你知道为什么吗?我想我以前没有遇到过这样的麻烦。感谢您的评论。
  • 加上额外的美元符号,它们是variable variables,所以在这两种情况下它们可能会转到$this-&gt;null并相互覆盖。

标签: php variables variable-assignment


【解决方案1】:

您正在使用variable variables,但您的代码看起来像是在尝试访问成员变量。

替换这个:

$this->$profile
$this->$account

有了这个

$this->profile
$this->account

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-07
    • 2011-05-07
    • 2021-03-22
    • 1970-01-01
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多