A Star
A Star

Reputation: 637

Looking for php files in two places and overriding the file if exists in the main directory

I would like to have some base files to be used across two sites, and then have them override the base file if there is a file found in lets call it the parent/main directory. Otherwise look in the secondary base directory.

I am developing a site that is pretty much a duplication, but there are only a few files that would be different. So in order to save me copying and pasting the whole site a feature like this would be very useful.

Has anybody come across a similar type of requirement, any information would be help.

I am also using Smarty Templating here.

Thanks

Upvotes: 0

Views: 76

Answers (1)

Tomasz Banasiak
Tomasz Banasiak

Reputation: 1560

Kohana Framework uses this type of structure. You've got 3-level directories:

  • application
  • modules
  • system

Kohana firstly searches for files in application folder, then in modules and in the end in system folder. Browse their code to see their solution for this problem.

The simple answer is to create own autoloader method, for example:

MAIN_DIR = '/my_base_dir';
ADDITIONAL_DIR = '/my_additional_dir';

function __autoload($class_name) {
     if (file_exists(MAIN_DIR . $class_name . '.php') {
         include MAIN_DIR . $class_name . '.php';
     } elseif (file_exists(ADDITIONAL_DIR . $class_name . '.php') {
         include ADDITIONAL_DIR . $class_name . '.php');
     } else {
         throw new Exception('Class ' . $class_name . ' not found!');
     }
}

And use it for loading PHP files.

Upvotes: 1

Related Questions