Why does this not make a difference between property and variable?

后端 未结 4 1980
旧时难觅i
旧时难觅i 2021-01-21 00:23
class someclass
{
    public $foo = \'abcd\';

    public function __construct($data)
    {
        $this->foo = $data;
    }
    public function doSomething()
    {
         


        
相关标签:
4条回答
  • 2021-01-21 00:30

    You need to use $this->foo to get and set the classes property

    0 讨论(0)
  • 2021-01-21 00:31

    When accessing class variables you need to use the $this-> prefix.

    Change your code to

    echo $this->foo = $_POST['foo'];
    var_dump($this->foo);
    
    0 讨论(0)
  • 2021-01-21 00:46

    It is correct and it works fine. I ran your code and it always gives me the same. There's no problem you have property $foo and $foo variable in one or multiple functions. It always give me the same answer.

    If $_POST['foo']=test then echo $foo = $_POST['foo']; returns "test", $foo returns "test" and $somethingelse returns "test";

    0 讨论(0)
  • 2021-01-21 00:48

    change this

    echo $foo = $_POST['foo'];
    

    to

    echo $this->foo = $_POST['foo'];
    var_dump($this->foo);
    
    0 讨论(0)
提交回复
热议问题