Jump to content

Abstract Class Vs All Static Methods


JustinK101

Recommended Posts

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();

Link to comment
https://forums.phpfreaks.com/topic/153244-abstract-class-vs-all-static-methods/
Share on other sites

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.

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.

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.

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()

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.