1
\$\begingroup\$

I'm developing a php framework called Stack. I just created the autoload.php as follows.

<?php
namespace Stack\Application;

define('ROOT', __DIR__);
class AutoLoader
{
    private static $pos = false;
    private static $namespace_path = NULL;
    private static $namespace_name = NULL;
    private static $file = NULL;
    /**
     * Set qualified name Stack\Application
     */
    public static function register()
    {
        spl_autoload_register('Stack\Application\AutoLoader::useNamespace');
    }
    public function unregister()
    {
        spl_autoload_unregister('Stack\Application\AutoLoader::useNamespace');
    }
    private static function useNamespace($namespace)
    {
        /**
         * @var $namespace namespace name
         * @var $pos — Find the position of the last occurrence of a class in a fully qulified $namespace
         */
        if (false !== $pos = strrpos($namespace, '\\')) {
            $pos            = strrpos($namespace, '\\');
            $namespace_path = str_replace('\\', DIRECTORY_SEPARATOR, substr($namespace, 0, $pos)) . DIRECTORY_SEPARATOR;
            $namespace_name = substr($namespace, $pos + 1);
            $file           = ROOT . DIRECTORY_SEPARATOR . '../..' . DIRECTORY_SEPARATOR . $namespace_path . $namespace_name . '.php';
            if (!file_exists($file)) {
                return false;
            }
            include $file;
        }
    }
}

Bootstrap.php

<?php

require_once __DIR__.'/AutoLoader.php';

use Stack\Application\AutoLoader as App;

App::register();

?>

index.php

<?php

    require_once __DIR__.'/../Application/Bootstrap.php';

    $model = new Stack\Application\Models\Model();

    $controller = new Stack\Application\Controllers\Controller($model);

    $view = new  Stack\Application\Views\View($model);

    echo $view->output();

    ?>

My directory structure of the framework as follows

 - Stack
 --Application
 ---configs      
 ---docs   
 ---layouts 
 ---modules   
 ---Views
 ---Controllers  
 ---forms  
 ---Models   
 ---services
 ---AutoLoader.php
 ---Bootstrap.php 
 --data  
 --docs  
 --library  
 --public
 ---index.php  
 --scripts  
 --temp

I'm going to keep same namespace name for the corresponding directory structure. Am i doing this right ?

\$\endgroup\$
  • 3
    \$\begingroup\$ Why don't you use composer for autoloading? \$\endgroup\$ – tkausl Nov 14 '15 at 12:04
  • \$\begingroup\$ But lot of frameworks are still using autoload \$\endgroup\$ – underscore Nov 14 '15 at 12:33
  • \$\begingroup\$ A lot of frameworks use composer for autoloading \$\endgroup\$ – tkausl Nov 14 '15 at 12:36
  • 1
    \$\begingroup\$ A lot of frameworks use custom autoloads only when it need some additional logic for autoloading classes, you don't need it, use conposer autoloader \$\endgroup\$ – user89569 Nov 14 '15 at 14:32
  • 1
    \$\begingroup\$ Don't reinvent the wheel (composer) unless you can do it better. Spend your time inventing wheels that don't already exist. \$\endgroup\$ – AD7six Nov 15 '15 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.