What does the variable $this mean in PHP?

前端 未结 10 1782

I see the variable $this in PHP all the time and I have no idea what it\'s used for. I\'ve never personally used it.

Can someone tell me how the variab

10条回答
  •  甜味超标
    2020-11-22 05:56

    I know its old question, anyway another exact explanation about $this. $this is mainly used to refer properties of a class.

    Example:

    Class A
    {
       public $myname;    //this is a member variable of this class
    
    function callme() {
        $myname = 'function variable';
        $this->myname = 'Member variable';
        echo $myname;                  //prints function variable
        echo $this->myname;              //prints member variable
       }
    }
    

    output:

    function variable
    
    member variable
    

提交回复
热议问题