Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 36585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:24:58+00:00 2026-05-10T14:24:58+00:00

I’m wondering what the best practice is for handling the problem with having to

  • 0

I’m wondering what the best practice is for handling the problem with having to ‘include’ so many files in my PHP scripts in order to ensure that all the classes I need to use are accessible to my script.

Currently, I’m just using include_once to include the classes I access directly. Each of those would include_once the classes that they access.

I’ve looked into using the __autoload function, but hat doesn’t seem to work well if you plan to have your class files organized in a directory tree. If you did this, it seems like you’d end up walking the directory tree until you found the class you were looking for. Also, I’m not sure how this effects classes with the same name in different namespaces.

Is there an easier way to handle this?

Or is PHP just not suited to ‘enterprisey‘ type applications with lots of different objects all located in separate files that can be in many different directories.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T14:24:59+00:00Added an answer on May 10, 2026 at 2:24 pm

    I my applications I usually have setup.php file that includes all core classes (i.e. framework and accompanying libraries). My custom classes are loaded using autoloader aided by directory layout map.

    Each time new class is added I run command line builder script that scans whole directory tree in search for model classes then builds associative array with class names as keys and paths as values. Then, __autoload function looks up class name in that array and gets include path. Here’s the code:

    autobuild.php

    define('MAP', 'var/cache/autoload.map'); error_reporting(E_ALL); require 'setup.php'; print(buildAutoloaderMap() . ' classes mapped\n');  function buildAutoloaderMap() {     $dirs = array('lib', 'view', 'model');     $cache = array();     $n = 0;     foreach ($dirs as $dir) {         foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)) as $entry) {             $fn = $entry->getFilename();             if (!preg_match('/\.class\.php$/', $fn))                 continue;             $c = str_replace('.class.php', '', $fn);             if (!class_exists($c)) {                 $cache[$c] = ($pn = $entry->getPathname());                 ++$n;             }         }     }     ksort($cache);     file_put_contents(MAP, serialize($cache));     return $n; } 

    autoload.php

    define('MAP', 'var/cache/autoload.map');  function __autoload($className) {     static $map;     $map or ($map = unserialize(file_get_contents(MAP)));     $fn = array_key_exists($className, $map) ? $map[$className] : null;     if ($fn and file_exists($fn)) {         include $fn;         unset($map[$className]);     } } 

    Note that file naming convention must be [class_name].class.php. Alter the directories classes will be looked in autobuild.php. You can also run autobuilder from autoload function when class not found, but that may get your program into infinite loop.

    Serialized arrays are darn fast.

    @JasonMichael: PHP 4 is dead. Get over it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.