How to handle the necessary classes in PHP

I am wondering how best to deal with the problem: “include” so many files in my PHP scripts to ensure that all the classes that I need to use are available for my script.

I am currently just using include_once to include classes that I access directly. Each of them will be the include_onceclasses to which they refer.

I learned how to use a function __autoload, but the hat does not work if you plan to organize class files in a directory tree. If you did, it looks like you will end up walking through the directory tree until you find the class you were looking for. Also, I'm not sure how these are effect classes with the same name in different namespaces.

Is there an easier way to handle this?

Or PHP is simply not suitable for applications like " enterpriseisey " with many different objects, all of which are in separate files, which can be in many different directories.

+3
source share
7 answers

setup.php, (.. ). .

, , script, , . __autoload . :

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]);
    }
}

, [class_name].class.php. autobuild.php. , , , .

.

@JasonMichael: PHP 4 . .

+6

spl_autoload_register:

spl_autoload_register('load_controllers');
spl_autoload_register('load_models');

function load_models($class){
    if( !file_exists("models/$class.php") )
        return false;

    include "models/$class.php";
    return true;
}
function load_controllers($class){
    if( !file_exists("controllers/$class.php") )
        return false;

    include "controllers/$class.php";
    return true;
}
+2

, , . Zend Zend Framework. , Zend_Loader::loadClass("Zend_Db_Table");, , , Zend_Loader .

Zend, , , Zend MVC.

, , . , Zend_Loader .

PHP, . :

ANY Dynamic class loader APC , , . APC .

+1

__autoload , , , . MVC , , .

, __autoload .

0

__autoload , PHP 5.

0

, . __autoload.

  • . , .. classes/User.php classes/User.class.php.
  • , .. , MVC, .
  • . , , Model_User, classes/Model/User.php. __autoload , , .
  • . __autoload, PHP , , classes , . , User, , classes/User.php classes/Models/User.php classes/Utility/User.php. User.php - classes, , , User .
0

@Kevin:

, spl_autoload_register - __autoload, , . , , __autoload.

? -:

__autoload, __autoload. , spl_autoload_register() - __autoload spl_autoload(), spl_autoload_call().

= > __autoload. , , , .

0

Source: https://habr.com/ru/post/1696507/


All Articles