Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在子文件夹中自动加载_Php - Fatal编程技术网

Php 在子文件夹中自动加载

Php 在子文件夹中自动加载,php,Php,我的文件夹结构如下: Root +--Level1 | +--index.php | | | \--Level2 | +--myclass.php index.php: namespace Level1; spl_autoload_extensions('.php'); spl_autoload_register(); Level2\myClass::myMethod(); class.php: namespace Level1\Level2; class myCl

我的文件夹结构如下:

Root
+--Level1
|  +--index.php
|  |
|  \--Level2
|     +--myclass.php
index.php:

namespace Level1;

spl_autoload_extensions('.php');
spl_autoload_register();

Level2\myClass::myMethod();
class.php:

namespace Level1\Level2;
    class myClass{
    ...
    }
我想在index.php中使用myclass.php中的类

但是当我调用该类时,我有一个错误:

LogicException:无法加载类Level1\Level2\myClass

它似乎试图加载level1\level2\myclass.php,但我已经在level1,所以它应该只加载level2\myclass.php(否则完整路径将是root\level1\level1\level2\myclass.php)


我哪里做错了?

问题是“自动加载程序”不“知道”您的“根”目录

“默认”的工作方式是将指定的“小写”名称空间作为directores追加,并将其作为目录追加到中的目录中

注意:它不使用“DOCUMENT\u ROOT”。

要解决您遇到的问题,您必须将您的“根”路径包含在PHP的“include\u路径”中。

下面是一些将“Root”目录添加到“include_path”的代码

我的“根”目录是'testmysql'

“level1\level2”中的“类”文件:

namespace Level1\Level2;

class myClass {

    public static function staticLocation()
    {
        echo '----------  ', '<br />';
        echo 'method    : ', __METHOD__, '<br />';
        echo 'class     : ', __CLASS__, '<br />';
        echo 'namespace : ', __NAMESPACE__, '<br />';
        echo 'directory : ', __DIR__, '<br />';
        echo 'filepath  : ', __FILE__, '<br />';
    }
}

好的,首先,
class
是一个保留关键字。当你做
class
-php会讨厌你,
class(){}
也不是你在php中定义类的方式,你不需要
()
。首先验证示例中的代码,并用有效的问题更新问题。是的,很抱歉,这只是一个占位符名称,我简化了脚本…感谢您的完整答案,我会很完美,所以我所要做的就是添加这行:
ini_集('include_path',ini_get('include_path');'。$\u服务器['DOCUMENT u ROOT']“;”);
namespace Level1;

spl_autoload_extensions('.php');
spl_autoload_register();

echo '<br />Document Root: ', $_SERVER['DOCUMENT_ROOT'], '<br />';

// Define the root path to my application top level directory (testmysql)
define('APP_ROOT',  $_SERVER['DOCUMENT_ROOT'] .'/testmysql');

echo '<br />', 'My Application Root directory: ', APP_ROOT, '<br />';

echo '<br />', 'This File: ', __FILE__, '<br />';
echo '<br />', 'Current Directory : ', __DIR__, '<br />';

echo '<br />', 'Include path (before): ', ini_get('include_path'), '<br />';
ini_set('include_path', ini_get('include_path') .';'. APP_ROOT .';');
echo '<br />', 'Include path (after): ', ini_get('include_path'), '<br />';

Level2\myClass::staticLocation();
P:/developer/xampp/htdocs

My Application Root directory: P:/developer/xampp/htdocs/testmysql

This File: P:\developer\xampp\htdocs\testmysql\level1\index.php

Current Directory : P:\developer\xampp\htdocs\testmysql\level1

Include path (before): .;P:\developer\xampp\php\PEAR

Include path (after): .;P:\developer\xampp\php\PEAR;P:/developer/xampp/htdocs/testmysql;
----------
method : Level1\Level2\myClass::staticLocation
class : Level1\Level2\myClass
namespace : Level1\Level2
directory : P:\developer\xampp\htdocs\testmysql\level1\level2
filepath : P:\developer\xampp\htdocs\testmysql\level1\level2\myClass.php