Jump to content

Way of accessing static variables inside class with expression returns?


Slips

Recommended Posts

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!

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.

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.

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  :D

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.