Jump to content

Way of accessing static variables inside class with expression returns?


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

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.