Jump to content

Static, Global


shamuntoha

Recommended Posts

Please don't use global to being variables into your function or even worse, a class. If you have data that is that closely related to the operation of the function/class, you should be using a class and the variables should be class variables and not main program variables.

Link to comment
Share on other sites

Thanks to all, but i read a article that storing inside class, cause memory or performance issues ? like not all programs need OOP.

 

Then don't use OOP?  I honestly don't think that there's a real memory hit in storing an int referenced by a class or just a random int in the main scope of the script...

Link to comment
Share on other sites

Thanks to all, but i read a article that storing inside class, cause memory or performance issues ? like not all programs need OOP.

 

It depends on what you are doing. Classes can be very efficient for certain tasks and not slow it down. Yes, using many many classes that are unecessary and instantiating them all at once is slow.

 

But for certain tasks, such as a DB connection, where you want to handle errors and have an easy way to get data out without having to repeat 10-15 lines of code each time is well worth it.

 

Just think, "Do I really need this class?". For a game script a class can be very nice cause you can use it to keep track of user data and what changed etc and only have to update it when needed, not to mention you can just keep passing the data via session using serialize and avoid having to make a ton of DB calls to keep retrieving data etc.

 

It just depends =)

Link to comment
Share on other sites

I would almost always choose the OOP route, just because it makes code way more manageable and understandable.  If you are writing very small simple scripts, than it is not really worth it.  But sometimes, small simple scripts turn in to large confusing ones.

 

If you are looking for performance, than maybe you should consider moving away from PHP.  If you are doing image processing and other CPU intensive things, you should write those programs in C or C++ and then exec them from the PHP.

 

"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil." - Knuth, Donald

Link to comment
Share on other sites

I'll second what flyhoney posted. You code for functionality first, optimization last.

 

If you are concerned about the few 10's of milliseconds or few 100's of bytes of memory that using OOP in php adds, then you should not be using slow parsed/tokenized/interpreted php code at all for your application.

Link to comment
Share on other sites

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.