Jump to content

Recommended Posts

I just started using php objects and have started to program in OOP (as best I can) but I have used static classes for so long I'm finding it hard to switch over to objects for 'everything' and not entirely sure if I should. So, I'd like to know from the veterans:

 

-) when should you use static classes over objects

 

I understand static classes are better for storing common data which should be accessed from any object, but that's about it.

 

What about say a method for displaying a date with an applied offset? - myClass::stamp2date(var $stamp); which can then be used within any object

 

OR

 

Should I be learning how to interface these objects with each other? It seems if I do that, I'm passing around too many objects needlessly because they are all accessing a common set of methods.

 

Any advice? or a link to where this is discussed in depth? I need somekind of guideline so I can discern when would be better to use one over the other (with obviously objects being the preferred strategy for most things).

 

Thanks.

The benefit of using a non-static method/property is the fact that you can use the public/protected/final/private methods & properties along with your current object.  If you have a static object.. the use of $this is of no use (as least I think I'm sure in that assumption).

 

Choosing whether you want a static or non-static object all boils down to whether you need to use a context.. such as $this.. or $parent....  It's actually pretty fortunate that you're so accommodated at using static classes, because that was actually the hardest concept for me to grab and usually the hardest concept to teach.

The benefit of using a non-static method/property is the fact that you can use the public/protected/final/private methods & properties along with your current object.  If you have a static object.. the use of $this is of no use (as least I think I'm sure in that assumption).

 

Choosing whether you want a static or non-static object all boils down to whether you need to use a context.. such as $this.. or $parent....  It's actually pretty fortunate that you're so accommodated at using static classes, because that was actually the hardest concept for me to grab and usually the hardest concept to teach.

 

Thanks for explaining that! You're right, you can only use $this within object context. Static equiv is self::$var.

 

I wish I did know exactly what php was thinking when using static classes - but I don't. I just know how to use them and that's it. I don't have a programming background so a lot of these concepts are lost on me. They are more a means to an end.

 

However, after reading a few highly recommended PHP OOP books, I am beginning to understand programming more.

 

I never used to care about concepts and stuff - I just knew what stuff did and used it if I needed to. That was it.

 

Now I've started seeing everything in my life as a number of objects interfacing with each other lol I'm obsessed and it's drove me crazy!

 

Thanks again. Would invite any other input too.

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.