What does the variable $this mean in PHP?

前端 未结 10 1766

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

    This is long detailed explanation. I hope this will help the beginners. I will make it very simple.

    First, let's create a class

    You can omit the php closing tag ?> if you are using php code only.

    Now let's add properties and a method inside Class1.

    The property is just a simple variable , but we give it the name property cuz its inside a class.

    The method is just a simple function , but we say method cuz its also inside a class.

    The public keyword mean that the method or a property can be accessed anywhere in the script.

    Now, how we can use the properties and the method inside Class1 ?

    The answer is creating an instance or an object, think of an object as a copy of the class.

    We created an object, which is $object1 , which is a copy of Class1 with all its contents. And we dumped all the contents of $object1 using var_dump() .

    This will give you

    object(Class1)#1 (2) { ["property1"]=> string(15) "I am property 1" ["property2"]=> string(15) "I am property 2" }
    

    So all the contents of Class1 are in $object1 , except Method1 , i don't know why methods doesn't show while dumping objects.

    Now what if we want to access $property1 only. Its simple , we do var_dump($object1->property1); , we just added ->property1 , we pointed to it.

    we can also access Method1() , we do var_dump($object1->Method1());.

    Now suppose i want to access $property1 from inside Method1() , i will do this

    property1;
        }
    }
    
    $object1 = new Class1;
    var_dump($object1->Method1()); 
    

    we created $object2 = new Class1; which is a new copy of Class1 or we can say an instance. Then we pointed to property1 from $object2

    return $object2->property1;
    

    This will print string(15) "I am property 1" in the browser.

    Now instead of doing this inside Method1()

    $object2 = new Class1;
    return $object2->property1;
    

    We do this

    return $this->property1;
    

    The $this object is used inside the class to refer to the class itself.

    It is an alternative for creating new object and then returning it like this

    $object2 = new Class1;
    return $object2->property1;
    

    Another example

    result = $this->property1 + $this->property2;
            return $this->result;
        }
    }
    
    $object1 = new Class1;
    var_dump($object1->Method1());
    

    We created 2 properties containing integers and then we added them and put the result in $this->result.

    Do not forget that

    $this->property1 = $property1 = 119

    they have that same value .. etc

    I hope that explains the idea.

    This series of videos will help you a lot in OOP

    https://www.youtube.com/playlist?list=PLe30vg_FG4OSEHH6bRF8FrA7wmoAMUZLv

提交回复
热议问题