timothyarden Posted April 24, 2014 Share Posted April 24, 2014 (edited) Hi Everyone,I am Struggling to understand the differences between "self", "$this", "::" and "->" and when / how to use them in classes. Any help would be appreciated, also if you could dumb it down to plain english that would help alot! Thanks in advance (In what I've been reading it's been saying stuff about static methods stuff and I'm not sure when / how this influences it - am I going down the wrong path?) Edited April 24, 2014 by timothyarden Quote Link to comment https://forums.phpfreaks.com/topic/287992-struggling-to-understand-the-differences-between-self-this-and-and-when-how-to-use-them/ Share on other sites More sharing options...
Jacques1 Posted April 24, 2014 Share Posted April 24, 2014 Hi, it's really simple: $this refers to the current object, and self refers to the current class. The -> operator is used to call a method or access an attribute of an object. And :: is used to access a static method or attribute of a class. <?php class A { public static $stat = 'Some static attribute'; public $inst = 'Some attribute of the instance'; public function test() { var_dump(self::$stat); // self refers to the class A var_dump($this->inst); // $this refers to the specific instance of A } } $a = new A(); $a->test(); There are some gotchas as to how self behaves in subclasses, but you should get the basic idea. Quote Link to comment https://forums.phpfreaks.com/topic/287992-struggling-to-understand-the-differences-between-self-this-and-and-when-how-to-use-them/#findComment-1477178 Share on other sites More sharing options...
timothyarden Posted April 24, 2014 Author Share Posted April 24, 2014 Thankyou for your post Jacques. Could you also explain what you mean by current object? (I understand current class). // I tried to look up objects but it didnt help http://www.php.net/manual/en/language.types.object.php Could you also explain what a method or attribute of an object is? "The -> operator is used to call a method or access an attribute of an object"; does this mean it can also be used in addition to :: to access static methods or an attribute of a class or is :: to be used exclusively for static methods and attributes of classes? Quote Link to comment https://forums.phpfreaks.com/topic/287992-struggling-to-understand-the-differences-between-self-this-and-and-when-how-to-use-them/#findComment-1477179 Share on other sites More sharing options...
timothyarden Posted April 24, 2014 Author Share Posted April 24, 2014 Also from your previous code will this code extract var_dump(self::$stat); // self refers to the class A var_dump($this->inst); // $this refers to the specific instance of A have the same effect as var_dump($this::$stat); // self refers to the class A var_dump(self->inst); // $this refers to the specific instance of A or have I missed the point Quote Link to comment https://forums.phpfreaks.com/topic/287992-struggling-to-understand-the-differences-between-self-this-and-and-when-how-to-use-them/#findComment-1477180 Share on other sites More sharing options...
Jacques1 Posted April 24, 2014 Share Posted April 24, 2014 You've missed the point. It's generally somewhat pointless to talk about details like this when you're not familiar with basics like objects, attributes and methods. There are many introductions for object-oriented PHP. You should start there. The first page I came accross is this one. Quote Link to comment https://forums.phpfreaks.com/topic/287992-struggling-to-understand-the-differences-between-self-this-and-and-when-how-to-use-them/#findComment-1477186 Share on other sites More sharing options...
timothyarden Posted April 24, 2014 Author Share Posted April 24, 2014 Okay, thanksI'll read it and get back to you Thanks for all your help Quote Link to comment https://forums.phpfreaks.com/topic/287992-struggling-to-understand-the-differences-between-self-this-and-and-when-how-to-use-them/#findComment-1477189 Share on other sites More sharing options...
timothyarden Posted April 25, 2014 Author Share Posted April 25, 2014 (edited) Still reading through the article. So far it has been a massive help. Thank you so much!I will probably still have some questions at the end of it though. Edited April 25, 2014 by timothyarden Quote Link to comment https://forums.phpfreaks.com/topic/287992-struggling-to-understand-the-differences-between-self-this-and-and-when-how-to-use-them/#findComment-1477241 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.