When would you use the $this keyword in PHP?

前端 未结 12 1729
长发绾君心
长发绾君心 2020-12-15 11:03

When would you use the $this keyword in PHP? From what I understand $this refers to the object created without knowing the objects name.

Al

相关标签:
12条回答
  • 2020-12-15 11:33
    <?php
      class identity {
        public $name;
        public $age;
        public function display() {
          return $this->name . 'is'. $this->age . 'years old';
        }
      }
    ?>
    
    0 讨论(0)
  • 2020-12-15 11:39

    A class may contain its own constants, variables (called "properties"), and functions (called "methods").

    <?php
    class SimpleClass
    {
        // property declaration
        public $var = 'a default value';
    
        // method declaration
        public function displayVar() {
            echo $this->var;
        }
    }
    ?>
    

    Some examples of the $this pseudo-variable:

    <?php
    class A
    {
        function foo()
        {
            if (isset($this)) {
                echo '$this is defined (';
                echo get_class($this);
                echo ")\n";
            } else {
                echo "\$this is not defined.\n";
            }
        }
    }
    
    class B
    {
        function bar()
        {
            // Note: the next line will issue a warning if E_STRICT is enabled.
            A::foo();
        }
    }
    
    $a = new A();
    $a->foo();
    
    // Note: the next line will issue a warning if E_STRICT is enabled.
    A::foo();
    $b = new B();
    $b->bar();
    
    // Note: the next line will issue a warning if E_STRICT is enabled.
    B::bar();
    ?>
    

    The above example will output:

    • $this is defined (A)
    • $this is not defined.
    • $this is defined (B)
    • $this is not defined.
    0 讨论(0)
  • 2020-12-15 11:43

    The use $this is to reference methods or instance variables belonging to the current object

    $this->name = $name or $this->callSomeMethod()

    that is going to use the variable or method implemented in the current object subclassed or not.

    If you would like to specifically call an implementation of of the parent class you would do something like

    parent::callSomeMethod()

    0 讨论(0)
  • 2020-12-15 11:46

    Used for when you want to work with local variables.

    You can also read more about it from here.

    function bark() {
        print "{$this->Name} says Woof!\n";
    }
    
    0 讨论(0)
  • 2020-12-15 11:48

    $this is used to make a reference to the current instance of an object. So you can do things like:

    class MyClass {
        private $name;
    
        public function setName($name) {
            $this->name = $name;
        }
    
        //vs
        public function setName($pName) {
            $name = $pName;
        }
    }
    

    Also another cool use is that you can chain methods:

    class MyClass2 {
        private $firstName;
        private $lastName;
    
        public function setFirstName($name) {
            $this->firstName = $name;
            return $this;
        }
    
        public function setLastName($name) {
            $this->lastName = $name;
            return $this;
        }
    
        public function sayHello() {
            print "Hello {$this->firstName} {$this->lastName}";
        }
    }
    
    //And now you can do:
    $newInstance = new MyClass2;
    $newInstance->setFirstName("John")->setLastName("Doe")->sayHello();
    
    0 讨论(0)
  • 2020-12-15 11:48

    One time I know I end up using the this equivalent in other languages is to implement a 'Fluent' interface; each class method which would otherwise return void instead returns this, so that method calls can be easily chained together.

    public function DoThis(){
        //Do stuff here...
        return $this;
    }
    public function DoThat(){
       //do other stuff here...
       return $this;
    }
    

    The above could be called like so:

    myObject->DoThis()->DoThat();
    

    Which can be useful for some things.

    0 讨论(0)
提交回复
热议问题