Anti-Moronic Posted March 6, 2010 Share Posted March 6, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/194346-static-classes-vs-objects-when-to-use-one-over-the-other/ Share on other sites More sharing options...
Zane Posted March 6, 2010 Share Posted March 6, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/194346-static-classes-vs-objects-when-to-use-one-over-the-other/#findComment-1022404 Share on other sites More sharing options...
Anti-Moronic Posted March 7, 2010 Author Share Posted March 7, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/194346-static-classes-vs-objects-when-to-use-one-over-the-other/#findComment-1022787 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.