douheng8629
2018-02-10 17:02
采纳率: 100%
浏览 84
已采纳

缺省的spl_autoload_register()命名空间行为,index.php在root之外

With modern PHP, calling

spl_autoload_register();

allows you to instantiate new classes without having to specify REQUIRE, INCLUDE nor USE within your php files, as long as they have a namespace, and as long as that namespace follows a folder/file structure such as:

|--models
     |-- utility
|           |___calculator.php
|
|
|--index.php

With such setup, if calculator.php has declared on its top "namespace models\utility", and it contains the class Calculator (a match to the file name calculator.php) you can automatically instantiate it from index.php by calling:

calc = New models\utility\Calculator();

However, if the folder structure is like this:

|--models
     |-- utility
|           |___calculator.php
|
|
|--public
     |--index.php

Now index.php can no longer access calculator through its namespace, as index.php is no longer at the root folder.

It seems that there is no way for public/index.php to reach namespaces above its level. It is just a limitation of PHP. Is there a way perhaps to register a function with spl_autoload_register that would retain its automatic, easy to work with behavior but allowing index.php to instantiate namespaces above its folder level? Or another way to deal with this situation while still never having to USE nor REQUIRE/INCLUDE files?


UPDATE WITH ANSWER

This is a directory agnostic solution that worked for me. I can successfully instantiate new classes merely by calling their namespace, without having to resort on USE, REQUIRE nor INCLUDE even though index.php was moved to the /public folder.

//'/../../../../' points the autoloader.php file back to root directory
$include_path = realpath(dirname(__FILE__) . '/../../../../');         
set_include_path($include_path);
spl_autoload_register();

图片转代码服务由CSDN问答提供 功能建议

使用现代PHP,调用

spl_autoload_register(); < / code>

允许您实例化新类,而不必在php文件中指定REQUIRE,INCLUDE和USE,只要它们具有命名空间,并且只要该命名空间跟随 文件夹/文件结构,例如:

  | --models 
 |  -  utility 
 |  | ___ calculator.php 
 | 
 | 
 | --index.php 
   
 
 

使用此类设置,如果calculator.php已在其顶部“名称空间声明 models \ utility“,它包含类Calculator(与文件名calculator.php匹配),您可以通过调用:

  calc =从index.php自动实例化它 新模型\实用程序\ Calculator(); 
   
 
 

但是,如果文件夹结构如下所示:

   | --models 
 |  - 实用程序
 |  | ___ calculator.php 
 | 
 | 
 | --public 
 | --index.php 
   
 
 

现在index.php无法再访问计算器 通过其命名空间,因为index.php不再位于根文件夹中。

似乎public / index.php无法达到其级别以上的命名空间。 这只是PHP的一个限制。 是否有一种方法可以使用spl_autoload_register注册一个函数,该函数将保留其自动,易于使用的行为,但允许index.php在其文件夹级别上实例化名称空间? 还是另一种方法来处理这种情况,同时仍然不必使用或请求/包含文件?


更新答案 < 这是一个目录无关的解决方案,对我有用。 我只能通过调用它们的命名空间来成功实例化新类,而不必使用USE,REQUIRE或INCLUDE即使index.php被移动到/ public文件夹。

  /  /'/../../../../'将autoloader.php文件指向根目录
 $ include_path = realpath(dirname(__ FILE__)。'/。/。/。/。  ./');  
set_include_path($ include_path); 
spl_autoload_register(); 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dosin84644 2018-02-10 17:17
    已采纳

    When the spl_autoload_register() function is called without the $autoload_function argument, the default autoloading function spl_autoload() is used.

    The spl_autoload() function checks all include paths for the file, matching namespaces to folders and class names to files as you have already noted.

    The key for you is to make sure that the folder containing the directory structure with your class files (i.e. the directory that contains models) is on the include path. This is commonly done using the set_include_path() function.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题