![]() |
Array and object performance
I decided it was time to continue working on my CMS again and am working on finishing up what was missing, and re-working a few tasks.
I have a component layer, which basically handles all the content tasks. Think Joomla for a second, if your familiar with it. Basically, I check if any component is set, and require its file if so. If the component has auto_start defined as true, and it is a class, the CMS will automatically call it. It used to pass about 6 objects to the constructor, the database abs, config, core functions obj, user obj, xml parser / cacher, and plugin object. I do this to prevent the need to global vars, but I dont like so many vars passed. So, I decided to trim this down. Before calling the component file, I packaged all vars into an array... $system['database'] = $database; $system['config'] = $config; etc etc etc Now I just pass this array to the constructor so its one var to deal with instead of setting and passing 6 or 7. Now my question, is wrapping an object into an array going to really hurt performance? So far, the CMS is fairly large scale, but runs extremely fast. Very few queries are executed.. infact, before the plugin layer, no queries were executed if there was a cached page to load ( cache also has option to make CMS work as flat file, stored as XML docs ). I want to keep the CMS running fast, and I didnt think an array with 6 or 7 keys would do much to performance, but wasnt sure. I als wasnt sure if writing ... if($system['user']->id > 0) { ... } if(!$system['database']->query($query)) { ... } Anyone have any input on which direction would be the better choice? |
The answer would be NO if...
Since mentioning Objects. A good idea about how to do this OOP way is by using Object patterns: http://en.wikipedia.org/wiki/Design_...puter_science) http://www.ibm.com/developerworks/li...AGX01&S_CMP=LP http://www.ibm.com/developerworks/op...GX44&S_CMP=EDU I read a good PHP based book about this topic PHP Objects, Patterns, and Practice by Matt Zandstra (Apress, 2008) This might give you some ideas, how to solve this. I know. I have the same problem every time. If I study to much about one problem, new problems will arise and I'll have to study even more. By making a good architecture you might lose some performance, but you'll gain more elsewhere. |
This sounds like a problem which could be easily solved using the registry pattern. Consider documenting about that, it will definitely improve your code :-)
|
Hi,
I advice using global variables. I know many will have raised eyebrows - but that is the best thing to do. With Global variables you will have a tradeoff with memory - but since your list is small - I think it should be fine. You should create an associative array for Global Variables so that you can load its value through the key. This will also ensure that you don't get mixed up with index numbers. Regards, Suniil |
What about using a Dependency Injection Container, good tutorial on DI and containers:
http://fabien.potencier.org/article/...ency-injection |
| All times are GMT. The time now is 01:59 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0