Slips Posted January 14, 2011 Share Posted January 14, 2011 Hi guys, I am trying something fairly simple but I'm not sure if this would be a good practice. Basically I am using a big class called CommonLibrary that holds common functions as methods and common variables as static variables. But I have some variables here and there like $allAlphabet = range ('a' , 'z'), that cannot be declared as a property because it gives me a parse error. I don't want to call an object for this class because instancing it is of no use. Values will never change with regards to instances. So the next best thing that I tried was declaring all static variables first, and then changing thei property values inside the class __construct with self::$variable = 'somevalue', and then using this code below to assign values to the empty static variables. $dummyObject = new CommonLibrary; unset($dummyObject); echo CommonLibrary::$staticVariable; // This property is NULL before the constructer is triggered. Anyone recommend any better ways of doing this? Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/224419-way-of-accessing-static-variables-inside-class-with-expression-returns/ Share on other sites More sharing options...
trq Posted January 14, 2011 Share Posted January 14, 2011 Anyone recommend any better ways of doing this? A registry object perhaps? I've never been a fan of using a class for simple storage (though that is pretty much what a registry is too). Quote Link to comment https://forums.phpfreaks.com/topic/224419-way-of-accessing-static-variables-inside-class-with-expression-returns/#findComment-1159330 Share on other sites More sharing options...
ignace Posted January 14, 2011 Share Posted January 14, 2011 I don't see why you need a utility class that holds the alphabet as a static variable. The alphabet isn't likely to change any time soon. Since you supposedly need this I think you haven't yet identified certain models or thought well about your OO-design. Quote Link to comment https://forums.phpfreaks.com/topic/224419-way-of-accessing-static-variables-inside-class-with-expression-returns/#findComment-1159476 Share on other sites More sharing options...
requinix Posted January 15, 2011 Share Posted January 15, 2011 Consider using things like global functions and constants. Unless you need a Registry or want to group functionality into one place, using a class - which I hope you marked as abstract - probably isn't necessary. It's just more memory in use. Quote Link to comment https://forums.phpfreaks.com/topic/224419-way-of-accessing-static-variables-inside-class-with-expression-returns/#findComment-1159578 Share on other sites More sharing options...
Slips Posted January 15, 2011 Author Share Posted January 15, 2011 Thanks for all the advice everyone, but after doing some reading and , for my application design this seems about right to go with Classname::init();, a static defined method that will initialize all the necessary variables in that class. I will probably change my designs and learn more over time and as my application grows Quote Link to comment https://forums.phpfreaks.com/topic/224419-way-of-accessing-static-variables-inside-class-with-expression-returns/#findComment-1159853 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.