开发者

php namespaces & autoload

I've read some posts about namespaces and autoload in php 5.3+, but still haven't succeeded in creating one working :x maybe some of you have an idea of what's going wrong about my code ? Thank you previously.

Autoloader.php class

<?php

    namespace my;

    class AutoLoader {

        private $aExt;
        private $sPath;
        protected static $instance;

        public static function getInstance() {
            if(!self::$instance instanceof self ) {
                self::$instance = new self();
            }
            return self::$instance;
        }

        function __construct($sPath = __DIR__, $exts = 'php') {
            // define path and extensions to include
            $this->setPath($sPath);
            $this->setExtensions($exts);
        }

        public function getPath() {
            return $this->sPath;
        }

        public function setPath($path){
            $this->sPath = $path;
        }

        public function removePath() {
            unset ($this->sPath);
        }

        public function addExtension($ext) {
            // prepends period to extension if none found
            $this->aExt[$ext]   = (substr($ext, 0, 1) !== '.') ? '.'.$ext : $ext;
        }

        public function removeExtension($ext) {
            unset ($this->aExt[$ext]);
        }

        public function getExtensions() {
            return $this->aExt;
        }

        public function setExtensions($extensions) {
            // convert
            if (is_string($extensions)) {
                $extensions = array($extensions);
            }

            // add
            foreach($extensions as $ext) {
                $this->addExtension($ext);
            }
        }

        public function register() {
            set_include_path($this->sPath);
            // comma-delimited list of valid source-file extensions
            spl_autoload_extensions(implode(',',$this->aExt));
            // default behavior without callback
            spl_autoload_register(array($this, 'autoload'));
        }


            public function autoload($sClassName) {
            include_once($sClassName.'.php');
            return;
        }

    }

    $autoloader = new AutoLoader();
    $autoloader->register();
?>

MyClass.php the class i am trying to load dinamically

<?php

    namespace my\tools;

    class MyClass {

        function __construct() {}

        function __destruct() {}

        function test() {
            echo 'ok';
        }
    }

?>

index.php the caller

<?php

    include_once('../Libraries/php/my/AutoLoader.php');

    new my\tools\MyClass();
?>

and finally the class structures on my disk

Libraries
  |_php
     |_my
     | |_Autolo开发者_开发问答ader.php
     |
     |_MyClass.php


That's a tad bit over-engineered, my friend.

You might want to take a look at simply using PSR-0 (PRS-0 is now depreciated, PSR-4 is the new one), an autoloader specification from a large number of PHP projects, like phpBB, Joomla, CakePHP, Zend Framework and lots more. It's built with namespaces in mind, but works well with or without them.

The advantage of PSR-0 (or PSR-4) is that it leads to a clean, simple, obvious directory structure that an increasing number of projects are supporting. This means using one autoloader instead of a single autoloader for every single set of code.


spl_autoload_register() expects a valid callback. You give ... something ^^ But not a callback. A callback is

// a closure
$cb = function ($classname) { /* load class */ }
// object method
$cb = array($object, 'methodName');
// static class method
$cb = array('className', 'methodName');
// function
$cb = 'functionName';

See manual: spl_autoload_register() for further information and examples.


After searching a little on PHP.net website, the solution were really simple :/

In fact php autoload function MUST be on root namespace /, mine was on first level of my package (my/), when i moved the class to root namespace everything worked fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜