aim25 Posted August 15, 2007 Share Posted August 15, 2007 Hi, a few days ago i started reading about oop in php. So i thought id give it a try. Ive gone through all the basic syntax's and scopes and magic methods and all, but one thing i can't get is the "$this" and the "->". Could some one give me an easy laymans definition please. Any help is appreciated, thanks ahead of time. Link to comment https://forums.phpfreaks.com/topic/65101-this-dont-understand/ Share on other sites More sharing options...
akitchin Posted August 15, 2007 Share Posted August 15, 2007 $this is a reserved variable name that points to an object from within itself - that is, an object can access its own variable MyVar by using $this->Myvar. the arrow (->) accesses the various methods and properties of an object. Link to comment https://forums.phpfreaks.com/topic/65101-this-dont-understand/#findComment-324894 Share on other sites More sharing options...
jishosan Posted August 15, 2007 Share Posted August 15, 2007 To expand on what was explained before: When using $this-> within a class definition for OOP, you are telling the system to reference the specific instance of a class rather than the actual function itself. For instance: <?php class Bar { var $text; function HelloWorld() { echo $this->text; } } $Foo = new Bar; $Foo->text = "Hello, World! <br>"; $Foo2 = new Bar; $Foo2->text = "Goodbye, World! <br>"; $Foo->HelloWorld(); $Foo2->HelloWorld(); ?> In the code above, we created two instances of the same class. When we called the HelloWorld() function for each class, the $this->text keyword replaced "$this" with the corresponding class name ($Foo and $Foo2). Link to comment https://forums.phpfreaks.com/topic/65101-this-dont-understand/#findComment-324977 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.