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

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
frank_mark
Forum Newbie
Posts: 13
Joined: Fri Jan 16, 2009 6:35 am

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

Post 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!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

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

Post by Christopher »

That is brilliant! Is this a story from The Onion? If not I am submitting it to Reddit! :drunk:
(#10850)
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

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

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

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

Post 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.
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

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

Post 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.
Warning: I have no idea what I'm talking about.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

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

Post 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.
Post Reply