PHP自动加载名称空间 [英] PHP autoload namespace

查看:61
本文介绍了PHP自动加载名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自动加载我的类,只放名称空间+文件名.

示例:

目录框架:

\var\www
  |_ foo
  |  |_ A.php
  |  |_ B.php
  |  
  |_ index.php

A.php:

<?php

namespace foo\A;

class A {

   private $a;

   public function __construct($a) {
       $this->a = $a;
   }

}

B.php:

<?php

namespace foo\B;

use foo\A;

class B extends A {

    private $b;

    public function __construct($a, $b) {
        parent::__construct($a);
        $this->b = $b;
    }   

}

index.php:

<?php

use foo\B;

define('ROOT', __DIR__ . DIRECTORY_SEPARATOR);

$b = new B('s', 2);

function __autoload($classname) {
    $namespace = substr($classname, 0, strrpos($classname, '\\'));
    $namespace = str_replace('\\', DIRECTORY_SEPARATOR, $classname);
    $classPath = ROOT . str_replace('\\', '/', $namespace) . '.php';

    if(is_readable($classPath)) {
        require_once $classPath;
    }
}

问题在于,在类A和BI中用类名声明了名称空间,当我使用它时,我打印了__autoload的变量,并且是正确的,当调用构造函数时bur,找不到该类. /p>

错误:

Fatal error: Class 'foo\A' not found in /var/www/foo/B.php on line 7

如果我仅实例化A,而我不使用B,则问题是相同的.

我需要这样做,因为我希望在B类中,如果不放置use语句,则不能使用A,以使其更加严格.

如果您能理解我的问题,我现在就不理解了,但是无论如何,谢谢您的建议!

PD:对不起,我的英语能力.

解决方案

您的代码应为类中的代码:

A.php

<?php

namespace foo;

class A {

   private $a;

   public function __construct($a) {
       $this->a = $a;
   }

}

B.php

<?php

namespace foo;

class B extends A {

    private $b;

    public function __construct($a, $b) {
        parent::__construct($a);
        $this->b = $b;
    }   

}

I want to autoload my classes putting only the namespace + the filename.

Example:

directories skeleton:

\var\www
  |_ foo
  |  |_ A.php
  |  |_ B.php
  |  
  |_ index.php

A.php:

<?php

namespace foo\A;

class A {

   private $a;

   public function __construct($a) {
       $this->a = $a;
   }

}

B.php:

<?php

namespace foo\B;

use foo\A;

class B extends A {

    private $b;

    public function __construct($a, $b) {
        parent::__construct($a);
        $this->b = $b;
    }   

}

index.php:

<?php

use foo\B;

define('ROOT', __DIR__ . DIRECTORY_SEPARATOR);

$b = new B('s', 2);

function __autoload($classname) {
    $namespace = substr($classname, 0, strrpos($classname, '\\'));
    $namespace = str_replace('\\', DIRECTORY_SEPARATOR, $classname);
    $classPath = ROOT . str_replace('\\', '/', $namespace) . '.php';

    if(is_readable($classPath)) {
        require_once $classPath;
    }
}

The problem is that in the class A and B I declare the namespace with the classname, and when I use it I print the variables of the __autoload and are correct, bur when call the constructor, don't find the class.

error:

Fatal error: Class 'foo\A' not found in /var/www/foo/B.php on line 7

If I only instantiate A, and I don't use B, the problem is the same.

I need to do it like this, because I want that in class B, you can't use A if you don't put the use statement, to do it more strict.

I don't now if you understand my problem for my explanation, but thanks anyway for any suggestion!!

PD: Sorry for my english skills.

解决方案

Your code should be that in classes :

A.php

<?php

namespace foo;

class A {

   private $a;

   public function __construct($a) {
       $this->a = $a;
   }

}

B.php

<?php

namespace foo;

class B extends A {

    private $b;

    public function __construct($a, $b) {
        parent::__construct($a);
        $this->b = $b;
    }   

}

这篇关于PHP自动加载名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆