【发布时间】:2015-05-04 19:27:35
【问题描述】:
我正在浏览一些遗留的php 代码库。我很难理解函数是如何在 php 中全局化的。这是项目的目录结构。
GlobalUtilities.php 中定义的函数在Profile.php 中可用,而没有任何include_once(../../../GlobalUtilies.php)。怎么会这样?
PHPProject
com.example.usermanagment
includes
GlobalUtilities.php
controller
Entrycontroller.php
model
classes
com
example
user
resources
Profile.php
但GlobalUtilies.php中包含这个文件autoloader.php。
`autoloader.php`
spl_autoload_register(function($class) {
$classFile = dirname(__DIR__)
. DIRECTORY_SEPARATOR . 'model'
. DIRECTORY_SEPARATOR . 'classes'
. DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';
if (file_exists($classFile)) {
include_once($classFile);
}
});
我查看了spl_autoload_register 的文档,我的理解是它不会在这里使函数成为全局函数
GlobalUtilies.php
require_once('autoloader.php');
......// rest of functions here
编辑:
我刚刚注意到GlobalUtilities.php 中没有关闭 php 标签。
也就是说,我只看到这个<?php 而没有看到?>。
是否会使此文件中定义的函数在全局范围内可用。
【问题讨论】:
-
自动加载器加载我相信的命名空间类。
-
好吧,虽然
Profile.php可能不包含GlobalUtilities.php,但Profile.php可能包含在还包含GlobalUtilies.php的文件中,可能类似于index.php。在这种情况下,它可以看到功能。 -
@JazzCat 是的,但是 PHP 目前缺少函数自动加载。
-
@Andrea:我看了
profile.php。它只有命名空间和其他use statements用于导入类。但没有任何形式的include或require -
这是一个框架吗?
标签: php function spl-autoload-register