php名称空间和自动加载

kit*_*sei 1 php namespaces autoload

我在php 5.3+中读过一些关于命名空间和自动加载的帖子,但是仍然没有成功创建一个工作:x也许你们中的一些人知道我的代码出了什么问题?谢谢你以前.

Autoloader.php

<?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();
?>
Run Code Online (Sandbox Code Playgroud)

MyClass.php 这个类我试图加载dinamically

<?php

    namespace my\tools;

    class MyClass {

        function __construct() {}

        function __destruct() {}

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

?>
Run Code Online (Sandbox Code Playgroud)

index.php 调用者

<?php

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

    new my\tools\MyClass();
?>
Run Code Online (Sandbox Code Playgroud)

最后是我磁盘上的类结构

Libraries
  |_php
     |_my
     | |_Autoloader.php
     |
     |_MyClass.php
Run Code Online (Sandbox Code Playgroud)

Cha*_*les 7

我的朋友,这有点过度设计了.

你可能想看一下简单地使用PSR-0(PRS-0现在被折旧, PSR-4是新的),来自大量PHP项目的自动加载器规范,如phpBB,Joomla,CakePHP,Zend Framework 还有更多.它在构建时考虑了名称空间,但无论是否使用它们都能很好地工作.

PSR-0(或PSR-4)的优点在于它可以提供一个干净,简单,明显的目录结构,以支持越来越多的项目.这意味着每一组代码都使用一个自动加载器而不是一个自动加载器.