Jump to content

Thoughts on multiple includes


drewbee

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :D

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.