Link to home
Start Free TrialLog in
Avatar of SevenAteAnthony
SevenAteAnthonyFlag for United States of America

asked on

Include Many Classes in One PHP File

Hi Experts,

Thanks for reading. How can I include many classes in my PHP files without many include statements?

<?php

include('db/connect.php');
include('lib/login_check.php');
include('lib/a.class.php');
include('lib/b.class.php');
include('lib/c.class.php');
include('lib/d.class.php');
include('lib/e.class.php');
include('lib/f.class.php');
include('lib/g.class.php');
include('lib/h.class.php');
include('lib/i.class.php');
include('lib/j.class.php');
include('lib/k.class.php');
include('lib/l.class.php');

Open in new window


Thanks!
Avatar of haloexpertsexchange
haloexpertsexchange
Flag of United States of America image

in theory you should be able to have as many as you want to.
just add them to the same file like you would add any other code, or what you could do is include all classes into a class file and then only include that one file when you want to use the classes.
create all_classs.php and put these into that file

include('lib/a.class.php');
...
include('lib/l.class.php');

Open in new window


then your page will be :) a bit better huh...

include('db/connect.php');
include('lib/login_check.php');
include('lib/all_classs.php');

Open in new window

Avatar of DrDamnit
I have done things like this before to load all the classes in a directory

$h = opendir(".");
while(($file = readdir($h)) !== false)
{
        //include the class... if it is a class.
        if(is_class($file))
        {
              include($file);
        }
}

Open in new window



where is_class() is a function that uses either preg_match or some other way of determining that your file is a class to be included. (Like... it ends with ".class.php").
By looking at these names - are those classes so different from each other ?
Avatar of SevenAteAnthony

ASKER

@Roads_Roads They're just example names. Not actual classes. :)
@SevenAteAnthony:

What's wrong with putting all the classes in a directory, and looping through them? It's a "plug-in" style of loading tons of classes without having to add the include statement every time.
@DrDamnit I see your logic, but how exactly would one go about doing this? Does this use an __autoloader?
It depends on the structure of your site / project.

Most of my projects include a config.php file that sets up an environment for the pages to operate within. This is a standard practice for most robust php projects (MediaWiki, Wordpress, osCOmmerce, etc...) I add this in teh config.php file and define the directory that holds the classes.

One advantage that I have discovered is that this allows me to setup debugging quite easily because I can insert debug statements in the loop and get information about certain classes.
@DrDamnit Could you give me an example of how this is done using include or require or whichever is utilized? Thank you.
ASKER CERTIFIED SOLUTION
Avatar of SevenAteAnthony
SevenAteAnthony
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Chosen as solutions above I was not looking for.