I do not assume that you are using object-oriented programming, but in case you are here, this might be a good answer.
In php, you can define a function called autoloader, if you try to create an object of a class that has not been defined, the autoloader is called. You can then use the class name to find out where the file containing this class is stored, so you can include it at the last moment. Here is an example.
<?php function on_load($class) { if(file_exists(require_once('classes/'.$class.'.php'))) { require_once('classes/'.$class.'.php'); } else { throw new Exception('Class not found: '.$class.' in classes/'); } } spl_autoload_register('on_load');
If you are working on a large project, you may want to group your files as follows
/classes/database/MySQL.php /classes/database/PDO.php // I'm just listing random stuff /classes/Core.php // Whatever /classes/datastructure/HashMap.php
Then you can use a special naming convention to find the correct directory
class Database_MySQL{} // look in <root_dir>/database/ for MySQL.php class Core // look in <root_dir>/ for Core.php class Database_Driver_Adapter_Useless_MysqlAdapterThingy {} // look in <root_dir>/Database/Driver/... blabla
Or you can use php 5.3 method and define your classes as follows
<?php namespace database\driver\adapter\useless; use database\driver\adapter\MysqlAdapter;
Now you need to use the 'use' keyword in every file you need. The best part is that the namespace is automatically added to the class name for your autoload function, so you can do something like this
function on_load($class) { require_once('root/' . str_replace('\\', '/' $class)); }
If you want to know more, try googeling the PHP download, there are tons of information on this. But then again. From the format of your question, I do not assume that you are using OOP, so this answer is only for people who found this question on Google.
Edit
I would also like to add the following:
// Only include the file once even if you place this statement multiple times require_once('bla.php'); include_once('bla.php'); require('bla.php'); // Error if file doesn't exist, php will not continue inlcude('bla.php'); // Warning if file doesn't exist, but php will continue
- Using include or require without _once means the file will be included every time the statement is executed
- Use include for templates or user-created files, use require_once for classes