drewbee Posted April 2, 2008 Share Posted April 2, 2008 Hello everyone. I am looking for some thoughts on performance, and possibly a better way (or opinions) on my current architecture. I have 4 objects (4 object calls on form pages, 2 object calls on form-less pages). I currently use the smarty templating system, so it is my super super super super class. :grin: Lets get a basic setup on a forms page (since it will be the heaviest with 4 object calls): Smarty Object (templating system; absolute parent class) Page object extends Smarty (Page is a generic class that does my database connections, as well as certain permission checks, session management etc.) Form extends Page (Form is a generic class that does auto-cleaning of forms Register extends Form (User account Registration form (processing & validation only; display is handled by smarty). So, why all these extensions? Well, I originally had it seperated into Smarty > Page and Form > Register Each page required two includes and two new objects, AND when I wanted to use the database in 'register', I had to pass the 'page' object to it by reference. Before I go any farther, is there a lot of issues with this type of setup? Everything seems to be running blazingly fast, but it is on my test server too. As well, I make a require() call to the parent class in each invidual file IE Page.class (require smarty) Form.class (require Page) Register.class (require Form); with this setup, in register.php I only need to require(register.class) as the cascading includes takes care of what is needed. Is this a good setup, or is it better to include all files in the file where I am going to be using them, IE register.php: require(smarty.class) require(Page.class) require(Form.class) require(register.class) 4 requires on one page, so it is no longer staggered. What are your thoughts and best practices for doing this type of setup? Quote Link to comment https://forums.phpfreaks.com/topic/99112-thoughts-on-multiple-includes/ Share on other sites More sharing options...
Daniel0 Posted April 2, 2008 Share Posted April 2, 2008 There are a couple of ways you could do it: 1. Include all the files in one file (e.g. index.php or something like that). 2. Let all the class files include their own dependencies (use require_once() so you won't risk including them twice) and then include the specific class you need on a page. 3. Use an autoloader. Also, why are you giving your files the extension .class? It's quite uncommon and if you place them inside the document root then the source will be exposed on a default system. Some people do, for some reason, choose to name classes like *class_name*.class.php. Quote Link to comment https://forums.phpfreaks.com/topic/99112-thoughts-on-multiple-includes/#findComment-507179 Share on other sites More sharing options...
drewbee Posted April 2, 2008 Author Share Posted April 2, 2008 Thanks for the insight; This was just sudo-code and doesn't reflect my current architecture. (was just a quick and easy way of describing things). My classes are actually setup like this: /includes/classes/classname.class.php The autoloader seems to be a good idea, I wonder what type of overhead this may cause though. Currently I am using what you have listed as #2 (each class calls its superclass). I guess the real question, is any method more efficient then another? I could also look into the ini_set('include_path') and set it to my class folder. Quote Link to comment https://forums.phpfreaks.com/topic/99112-thoughts-on-multiple-includes/#findComment-507464 Share on other sites More sharing options...
Daniel0 Posted April 2, 2008 Share Posted April 2, 2008 The way I do is like this: - Add my library folder to the include path - Have an autoloader sort of like this: function __autoload($className) { require_once str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; } So e.g. a class like PHPFreaks_Auth_Adapter_SMF would be in library/PHPFreaks/Auth/Adapter/SMF.php. I find that to work really well. Quote Link to comment https://forums.phpfreaks.com/topic/99112-thoughts-on-multiple-includes/#findComment-507488 Share on other sites More sharing options...
drewbee Posted April 2, 2008 Author Share Posted April 2, 2008 Nice idea. Where do you usually house this autoload function? From what I can see, I would want to place this in my Page class. Quote Link to comment https://forums.phpfreaks.com/topic/99112-thoughts-on-multiple-includes/#findComment-507543 Share on other sites More sharing options...
Daniel0 Posted April 2, 2008 Share Posted April 2, 2008 Well you could put it in your index.php. I usually use Zend Framework, so I just do require_once 'Zend/Loader.php'; Zend_Loader::registerAutoload(); That will register Zend_Loader::autoload() with spl_autoload_register(). Zend_Loader::autoload() calls Zend_Loader::loadClass() when a class is attempted to be autoloaded. Quote Link to comment https://forums.phpfreaks.com/topic/99112-thoughts-on-multiple-includes/#findComment-507553 Share on other sites More sharing options...
maexus Posted April 4, 2008 Share Posted April 4, 2008 I have a master include file which searchings folders for non class includes using glob() and the class files are included using autoload. Then I just have to include the master file. I keep the non class includes to a minimum which keeps it running pretty fast. This master file gives me the ability to drag and drop new includes without modifying code. BTW, it's important to use require_once or include_once with something like this. Quote Link to comment https://forums.phpfreaks.com/topic/99112-thoughts-on-multiple-includes/#findComment-509360 Share on other sites More sharing options...
drewbee Posted April 4, 2008 Author Share Posted April 4, 2008 Right. I understand the importance of require_once. Currently I just do it through the normal require, and at worse if I call it twice I know it right away due to the class re-declaration error message. I also code with E_ALL error reporting level, so it is safe regardless. My scripts scream bloody murder at the littlest of things Quote Link to comment https://forums.phpfreaks.com/topic/99112-thoughts-on-multiple-includes/#findComment-509374 Share on other sites More sharing options...
keeB Posted April 10, 2008 Share Posted April 10, 2008 I am really keen on the way Java does things, and it makes things much more readable (for me!) if all of the includes are listed at the top of the file. Quote Link to comment https://forums.phpfreaks.com/topic/99112-thoughts-on-multiple-includes/#findComment-513517 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.