What does the variable $this mean in PHP?

前端 未结 10 1597

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:50

    when you create a class you have (in many cases) instance variables and methods (aka. functions). $this accesses those instance variables so that your functions can take those variables and do what they need to do whatever you want with them.

    another version of meder's example:

    class Person {
    
        protected $name;  //can't be accessed from outside the class
    
        public function __construct($name) {
            $this->name = $name;
        }
    
        public function getName() {
            return $this->name;
        }
    }
    // this line creates an instance of the class Person setting "Jack" as $name.  
    // __construct() gets executed when you declare it within the class.
    $jack = new Person("Jack"); 
    
    echo $jack->getName();
    
    Output:
    
    Jack
    
    0 讨论(0)
  • 2020-11-22 05:52

    $this is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object).

    0 讨论(0)
  • 2020-11-22 05:55

    The best way to learn about the $this variable in PHP is to try it against the interpreter in various contexts:

    print isset($this);              //true,   $this exists
    print gettype($this);            //Object, $this is an object 
    print is_array($this);           //false,  $this isn't an array
    print get_object_vars($this);    //true,   $this's variables are an array
    print is_object($this);          //true,   $this is still an object
    print get_class($this);          //YourProject\YourFile\YourClass
    print get_parent_class($this);   //YourBundle\YourStuff\YourParentClass
    print gettype($this->container); //object
    print_r($this);                  //delicious data dump of $this
    print $this->yourvariable        //access $this variable with ->
    

    So the $this pseudo-variable has the Current Object's method's and properties. Such a thing is useful because it lets you access all member variables and member methods inside the class. For example:

    Class Dog{
        public $my_member_variable;                             //member variable
    
        function normal_method_inside_Dog() {                   //member method
    
            //Assign data to member variable from inside the member method
            $this->my_member_variable = "whatever";
    
            //Get data from member variable from inside the member method.
            print $this->my_member_variable;
        }
    }
    

    $this is reference to a PHP Object that was created by the interpreter for you, that contains an array of variables.

    If you call $this inside a normal method in a normal class, $this returns the Object (the class) to which that method belongs.

    It's possible for $this to be undefined if the context has no parent Object.

    php.net has a big page talking about PHP object oriented programming and how $this behaves depending on context. https://www.php.net/manual/en/language.oop5.basic.php

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题