Sentido Web

Sentido Web es un blog sobre desarrollo web, en el que se comparten referencias y explicaciones sobre PHP, Ajax, XHTML, MySQL y mucho más...

10Nov
09

Simular paquetes en PHP

Interesante script que nos permite cargar paquetes al estilo Java desde PHP. Aunque ya existen los phar, que es un paquete de PHP, esto puede resultar una posibilidad o al menos un método a tener en cuenta para importar varios archivos simultáneamente.

<?php
 
$basePath = "./";
 
function import($classPath)
{
    global $basePath;
 
    /* If the path ends with a '.*' then include
       all the files in the last given directory.
    */
    if(preg_match('/(\.\*)$/', $classPath))
    {
        $importFilePath = substr($classPath, 0, strlen($classPath) - 2);
        $importFilePath = str_replace(".", "/", $importFilePath);
        $d= dir($basePath . $importFilePath);
 
        while(false !== ($file = $d->read())) 
        {
            /* Reject parent, current directories and sub directories */
            if(($file == '.')  ||
               ($file == '..') ||
               (is_dir($d->path . "/" . $file)))
            {
                continue;
            }
 
            require_once($basePath . $importFilePath . "/" . $file);
        }
    } else {
        /* If a single file is specified */
        $importFile = str_replace(".", "/", $classPath) . ".php";
    	require_once($basePath . $importFile);
    }
}
 
?>

Simulating Packages in PHP

Otras entradas

Los comentarios están cerrados.