silkfire Posted August 13, 2012 Share Posted August 13, 2012 I've been thinking about this for a while. In my classes I use self::method() for all methods, including non-static ones. Why does PHP allow this in the first place? What are best coding practices do you think? I haven't been able to find much about this by googling except for this but it didn't give me an intelligent answer. http://forums.devnetwork.net/viewtopic.php?f=1&t=113624 Quote Link to comment https://forums.phpfreaks.com/topic/267025-best-practice-selfmethod-or-this-method/ Share on other sites More sharing options...
xyph Posted August 13, 2012 Share Posted August 13, 2012 It's a loosely-typed language. Since an 'instance' will always exist within the class (the methods will always be available), there's no reason to differentiate. Personally, I'll use $this->whatever for all internal references... just because? I guess the 'better' one to use is the one your team is using, or the one in the rest of your code Quote Link to comment https://forums.phpfreaks.com/topic/267025-best-practice-selfmethod-or-this-method/#findComment-1369051 Share on other sites More sharing options...
scootstah Posted August 13, 2012 Share Posted August 13, 2012 It is a common misconception that self:: is only for static methods, and $this is only for non-static methods. This isn't entirely true, though, and both can be used in both ways. self:: refers to the current class, and $this refers to the current object. Consider this example: <?php class Car { public function getMake() { return 'Ford'; } public function getModel() { return 'Focus'; } public function getVehicle() { return $this->getMake() . ' ' . $this->getModel(); } } class Truck extends Car { public function getModel() { return 'F150'; } } If we create a new Truck object, and call the "getVehicle" method, it will output "Ford F150". <?php $truck = new Truck; echo $truck->getVehicle(); // Ford F150 It does so because $this refers to the current object, which is Truck. If we change $this->getModel() to self::getModel(), it will use the current class's (current being the class that self:: resides in, in this case Car) getModel method. <?php ... public function getVehicle() { return $this->getMake() . ' ' . self::getModel(); } ... $truck = new Truck; echo $truck->getVehicle(); // Ford Focus I hope that severely lackluster example helps you understand. Quote Link to comment https://forums.phpfreaks.com/topic/267025-best-practice-selfmethod-or-this-method/#findComment-1369052 Share on other sites More sharing options...
KevinM1 Posted August 13, 2012 Share Posted August 13, 2012 That said, the convention is to use 'self' in a static context and 'this' in an instance context. Since just about everyone follows that convention, it's wise to stick to it. Quote Link to comment https://forums.phpfreaks.com/topic/267025-best-practice-selfmethod-or-this-method/#findComment-1369055 Share on other sites More sharing options...
scootstah Posted August 13, 2012 Share Posted August 13, 2012 It's also worth noting that when in a static context, static:: can sometimes be superior to self:: It implements late-static-binding. In a nut shell, that means that static:: works on the same idea as $this, in that it refers to the class in which it was called and not in which it resides (for lack of better terminology). So, in my earlier example if you changed self::getModel() to static::getModel(), you would end up with "Ford F150", just like with $this->getModel(). In addition to that, it makes unit testing a lot easier for static methods. Quote Link to comment https://forums.phpfreaks.com/topic/267025-best-practice-selfmethod-or-this-method/#findComment-1369056 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.