为什么类变量不存储php类中的值?

Par*_*nak 1 php class

我刚开始研究php中的类和对象.我有一个非常小的程序,如下所示.

<?PHP
class GetUserPermissions 
{
public $tab1;
public $tab2;
public $tab3;
public $tab4;

public function setMainPagePermissions()
{
    try
    {                   
        $this->SetPermissionsSelection(1,0,5,0);
    }
    catch(Exception $e)
    {
        echo $e->getMessage();
    }
}

public function SetPermissionsSelection($a,$b,$c,$d)
{       
    $this->$tab1=$a;
    $this->$tab2=$b;
    $this->$tab3=$c;
    $this->$tab4=$d;
}

 public function gettab1Status()
 {
    return $this->$tab1;
 }
  public function gettab2Status()
  {
  return $this->$tab2;

}
public function gettab3Status()
{
    return $this->$tab3;

}
public function gettab4Status()
{
    return $this->$tab4;
}

}

$test=new GetUserPermissions();
$test->setMainPagePermissions();

echo "<br>value 1 : ".$test->gettab1Status();
echo "<br>value 2 : ".$test->gettab2Status();
echo "<br>value 3 : ".$test->gettab3Status();
echo "<br>value 4 : ".$test->gettab4Status();
?>
Run Code Online (Sandbox Code Playgroud)

在此不打印类成员变量的值.

tam*_*asd 5

你有语法问题.在PHP中,您可以访问类成员$this->foo,而不是$this->$foo.后者使用$ foo变量的值来获取成员.