开发者

Why the class variables does not stores values in php class?

开发者 https://www.devze.com 2023-01-18 15:30 出处:网络
I just started study of class and object in php. I have a very small program which is as follows. <?PHP

I just started study of class and object in php. I have a very small program which is as follows.

<?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->setMainPagePermissio开发者_StackOverflow中文版ns();

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

In this its not prints the values of the class member variables.


You have a syntax problem. In PHP, you access the class members with $this->foo, not $this->$foo. The latter uses the $foo variable's value to get the member.


Remove dollars when accessing fields with $this->field

$this->tab1=$a;
$this->tab2=$b;
$this->tab3=$c;
$this->tab4=$d;
0

精彩评论

暂无评论...
验证码 换一张
取 消