Page 1 of 1

All my classes and methods are public static, do I suck?

Posted: Wed Mar 25, 2009 8:20 pm
by frank_mark
Hi guys,
I am just wrapping my head around OOP in php and it is working quite well form me and my code is becoming much more manageable and shorter as a result.

The only problem I have is that my brain can't understand why I would need to instantiate objects :? . All the methods in my classes are public static and I do not require multiple instances of and object. In this case should I be using OOP at all?

I keep reading that I should be instantiating the objects and using them however it seems my code is working just fine, but I am afraid that it will get really messed up if I'm not doing the right thing.

Can someone please clarify for me why I would benefit from instantiating the object.

Thanks
Frank

PS: I suck at programming despite all this, but I'm trying to improve... I swear!

Re: All my classes and methods are public static, do I suck?

Posted: Wed Mar 25, 2009 8:30 pm
by Christopher
That is brilliant! Is this a story from The Onion? If not I am submitting it to Reddit! :drunk:

Re: All my classes and methods are public static, do I suck?

Posted: Wed Mar 25, 2009 8:35 pm
by Eran
It sounds like you are using classes as merely namespaces for functions. Objects are much more than that - they contain actual data. You can have several objects of the same class with different data - something you can't achieve using static methods alone (as all static methods share the same class). You might say you have not encountered the need for multiple objects of the same type - but I would guess then that you use arrays to replace that functionality. Objects allow you to tie data with functionality, which is a very powerful technique.

Another aspect is that static method and properties can not be inherited (as they are tied to a specific class name), and thus completely static classes lose the inheritance part of OOP. The reuse of code and structure through inheritance is a major part of what made OOP such a powerful methodology.

And since you say that all those method are public static, it means that you don't use visibility and encapsulation at all, and are missing out on more important OO advantageous.

Of course your code can "work just fine", that's not an indication of the strength of the code design. Procedural code can work just fine as well. If you really want to enjoy the benefits of OOP, you should start learning about what makes it so powerful.

Re: All my classes and methods are public static, do I suck?

Posted: Wed Mar 25, 2009 9:11 pm
by Chris Corbyn
Love it :)

The first two sentences are classic.
I am just wrapping my head around OOP in php and it is working quite well form me and my code is becoming much more manageable and shorter as a result.

The only problem I have is that my brain can't understand why I would need to instantiate objects
No offence, sriously :)

~pytrin hit the nail on the head.

Re: All my classes and methods are public static, do I suck?

Posted: Thu Mar 26, 2009 9:06 pm
by thinsoldier
Show us an example of one of your classes so we have a more accurate idea of what advice you need to get to the next level of understanding.

Re: All my classes and methods are public static, do I suck?

Posted: Fri Mar 27, 2009 3:53 pm
by Luke
I'd just like to make it clear (if pytrin didn't already) that merely using objects doesn't make your code object-oriented. You must understand the why before you attempt the how.