JustinK101 Posted April 8, 2009 Share Posted April 8, 2009 Is there a difference between: abstract class test { public function get() { } public function get2() { } } test::get(); test::get2(); AND class test { public static function get() { } public static function get2() { } } test::get(); test::get2(); Quote Link to comment https://forums.phpfreaks.com/topic/153244-abstract-class-vs-all-static-methods/ Share on other sites More sharing options...
redarrow Posted April 8, 2009 Share Posted April 8, 2009 http://uk2.php.net/abstract Quote Link to comment https://forums.phpfreaks.com/topic/153244-abstract-class-vs-all-static-methods/#findComment-805029 Share on other sites More sharing options...
JustinK101 Posted April 8, 2009 Author Share Posted April 8, 2009 redarrow, I have read that article, but I am struggling with the difference between making the entire class abstract and making each method simply static. I guess what's the difference? It seems like abstract is just a short cut to making every method in a class static. Quote Link to comment https://forums.phpfreaks.com/topic/153244-abstract-class-vs-all-static-methods/#findComment-805031 Share on other sites More sharing options...
Maq Posted April 8, 2009 Share Posted April 8, 2009 Here's the easiest way to explain what an abstract class does, from the manual: You use interfaces to decouple the behavior of a class from the accessors and mutators which make up the class. With an abstract class, you are saying that "Method X should work like this", "Variable Y should be used", an interface could be used to do something completely different. Quote Link to comment https://forums.phpfreaks.com/topic/153244-abstract-class-vs-all-static-methods/#findComment-805048 Share on other sites More sharing options...
AmandaF Posted April 9, 2009 Share Posted April 9, 2009 Static methods are methods that can be accessed without being associated with a specific object. (If I understand correctly, there isn't a whole lot of difference between making a static method and making a non-OO function). Abstract methods are something else entirely. They're methods that haven't been implemented (in other words, the method has a name but no code) inside abstract classes. Other classes inherit these classes and implement the methods. To borrow a metaphor from this site, you might have classes representing dog breeds, all of which inherit from the abstract "dog" class with an abstract "bark" method. Bark() may be implemented differently depending on the class (some dogs "yip", others "woof", etc) but all classes inheriting the dog class must have a bark method. It's basically a way of making sure that similar objects have similar functionality. Quote Link to comment https://forums.phpfreaks.com/topic/153244-abstract-class-vs-all-static-methods/#findComment-805815 Share on other sites More sharing options...
Mark Baker Posted April 10, 2009 Share Posted April 10, 2009 Static methods are methods that can be accessed without being associated with a specific object. (If I understand correctly, there isn't a whole lot of difference between making a static method and making a non-OO function).Static classes still have the benefit of scope for static attributes and constants defined within the class, so that they are accessible to all methods within the class without the need to use global or pass them as parameters to all "functions" that need them. You can also combine static and non-static methods within an instantiated class. I've noticed that self::methodName() is fractionally faster than $this->methodName() Quote Link to comment https://forums.phpfreaks.com/topic/153244-abstract-class-vs-all-static-methods/#findComment-806214 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.