Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.