首页 文章

PHP spl_autoload_register命名空间会干扰文件包含

提问于
浏览
0

我不知道我是否遗漏了一些东西,但使用命名空间似乎打破了我的应用程序 . 它将找不到父类,因为它在调用自动加载寄存器时在类名中包含命名空间

spl_autoload_register(function($classname){
    if (preg_match('/[a-zA-Z]+Controller$/', $classname)) {
        echo __DIR__ . '/controllers/' . $classname.".php" . "
"; require __DIR__ . '/controllers/' . $classname.".php"; } }); //echo produces: /var/www/web/controllers/DefaultController.php /var/www/web/controllers/Project\Controllers\BaseController.php

Project \ Controllers是默认和基本控制器中使用的命名空间 . 默认扩展基本控制器 .

为什么spl autoload这样做?

Structure:

web / controllers:BaseController.php

DefaultController.php

BaseController:

namespace Project\Controllers;

class BaseController
{
    private $config;

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

DefaultController:

<?php
namespace Project\Controllers;

class DefaultController extends BaseController
{
}

1 回答

  • 0

    主要问题似乎是您的自动加载器不知道它所属的根命名空间 . 因此,您的自动加载器应该知道或成为命名空间的一部分,因此在本例中为Project .

    所以你的自动加载需要做的第一件事就是删除已知的根命名空间(当然除非它被用作文件结构的一部分,在这种情况下它不是)

    <?php namespace Project;
    
    // Within the namespace
    $classname = ltrim(str_replace(__NAMESPACE__ . '\\', '', $classname), '\\');
    
    // Outside of the namespace
    $classname = ltrim(str_replace('Project\\', '', $classname), '\\');
    

    现在为类名和实际文件位置 .

    // find the last backslash
    if($last = strripos($classname, '\\')) {
        $namespace = substr($classname, 0, $last); // Controllers
        $classname = substr($classname, $last+1); // DefaultController
    }
    

    现在,您可以将实际文件位置构建到PHP类文件中

    $filepath = '/var/www/'; // Root
    if(isset($namespace)) {
        // Add the controllers bit onto the root
        $filepath .= str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
    }
    // add the class name (DefaultController) to the file path
    $filepath .= str_replace('\\', DIRECTORY_SEPARATOR, $classname) . '.php';
    

    这应该导致以下文件路径,如果存在则可以检查,如果存在则包括在内 .

    /var/www/Controllers/BaseController.php

    /var/www/Controllers/DefaultController.php

相关问题