shamuntoha Posted December 6, 2008 Share Posted December 6, 2008 function ..() { global $all_pages; } is global inside function, same like outside functions to define? Quote Link to comment Share on other sites More sharing options...
gevans Posted December 6, 2008 Share Posted December 6, 2008 A global variable is made available to all parts of a script. If you set the variable outside the function, you can set it to a global variable inside the function to make it available Quote Link to comment Share on other sites More sharing options...
shamuntoha Posted December 6, 2008 Author Share Posted December 6, 2008 index.php global $a; class.php function called_from_index() { & $a = 4; } do you mean from above, i have to use global to change the $a? Quote Link to comment Share on other sites More sharing options...
gevans Posted December 6, 2008 Share Posted December 6, 2008 yes, if you want to change $a within the function you have to set it as global, but you need to do this inside the function Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 6, 2008 Share Posted December 6, 2008 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. Quote Link to comment Share on other sites More sharing options...
shamuntoha Posted December 6, 2008 Author Share Posted December 6, 2008 Thanks to all, but i read a article that storing inside class, cause memory or performance issues ? like not all programs need OOP. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted December 6, 2008 Share Posted December 6, 2008 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... Quote Link to comment Share on other sites More sharing options...
waynew Posted December 6, 2008 Share Posted December 6, 2008 You won't have to worry about those kind of performance issues unless your site is popular, and by then you'll be rich enough to hire another programmer! =) Quote Link to comment Share on other sites More sharing options...
shamuntoha Posted December 6, 2008 Author Share Posted December 6, 2008 Many thanks for comments. some sites benchmerk show things slowing down while using lot of things which is not thought more before creating it, where i am doing a open source project for VML, XML, SVG, JS (respecting large hits). http://www.php.lt/benchmark/phpbench.php do we care those staff while doing php? Quote Link to comment Share on other sites More sharing options...
premiso Posted December 6, 2008 Share Posted December 6, 2008 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 =) Quote Link to comment Share on other sites More sharing options...
Mchl Posted December 6, 2008 Share Posted December 6, 2008 The benchmark you posted would be a lot more interesting, if it was running on some recent PHP version. A lot has changed since 4.0.6. Quote Link to comment Share on other sites More sharing options...
flyhoney Posted December 6, 2008 Share Posted December 6, 2008 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 Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 6, 2008 Share Posted December 6, 2008 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. Quote Link to comment 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.