设置包含路径和自动加载是两个不同的东西吗?
我注意到添加我需要的文件夹的包含路径不足以访问这些类。
我有一个文件夹 application/tests/ ,其中包含 application/tests/Test.php
类 Test.php 名为 Test ,没有任何前缀
class Test {
}
如果我仅在包含路径中添加测试文件夹
realpath(APPLICATION_PATH . '/tests')
类 Test.php 在控制器中不起作用
new Test()
那么将文件夹包含在包含路径中还不够吗?
I'm noticing that adding the include path of a folder I need is not enough to access the classes.
I have a folder application/tests/ which contains application/tests/Test.php
class Test.php is named Test without any prefixes
class Test {
}
If I add the tests folder only in the include path
realpath(APPLICATION_PATH . '/tests')
The class Test.php doesn't work in the controllers
new Test()
so isn't including the folder in the include path enough?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@StasM 说得对。但这里还有更多解释。
include_path
告诉 PHP 在执行include
、include_once
、require
或 <代码>require_once。当然,这些include
和require
始终引用特定的文件。自动加载专门针对缺少类。当在代码中引用尚未加载的类时(通常但不限于通过调用
new
运算符,如您的示例中所示),那么自动加载是一个可以通过以下算法启动的过程:加载类。此过程通常涉及从丢失的类的名称开始,并根据丢失的类的名称生成可能的路径名集合 - 文件系统中的绝对路径或相对于include_path
中的条目,然后执行执行include
希望类定义驻留在其中之一。就您而言,您调用了一个名为“Test”的类。包含此类的文件位于文件 APPLICATION_PATH 中。 '测试/Test.php'。并且您已放置目录 APPLICATION_PATH 。
include_path
中的“tests”。但在自动加载算法到位之前,系统无法将类名'Test'
连接到文件名'Test.php'
。PEAR 风格的类名约定 将提供此连接。采用该约定的自动加载器(例如默认的 Zend Framework 自动加载器)将能够执行从类名到文件名的映射,然后
include
所需的文件。最后一个复杂问题是,传统的 Zend Framework 目录布局约定将某些类放置在 不 包含路径上的文件夹中:模型、表单、服务、控制器、视图帮助程序、操作帮助程序等。这就是为什么您经常会看到自动加载器的更多配置 - 通常在
Bootstrap
类中 - 定义某些类名与文件系统中include_path 之外的某些位置之间的映射
。例如,乍一看,名为“Default_Model_User”的类可能会驻留在文件“Default/Model/User.php”的“include_path”中。但标准应用程序目录结构希望将该文件放置在“application/models/User.php”中。请注意路径名中的复数 'models' 和小写 'm',以及类名中存在的“Default”。为了理解这一点,需要对自动加载器进行额外的配置,并提供类名到文件名映射的模式。这通常是通过资源自动加载器来完成的。
希望这些对您或其他人有所帮助。干杯!
@StasM has it right. But here's a little more explanation.
The
include_path
tells PHP where to look when it executes aninclude
, aninclude_once
, arequire
, or arequire_once
. Of course, theseinclude
s andrequire
s always reference a particular file.Autoloading is specifically about missing classes. When an as-yet-unloaded class is referenced in code - typically, though not exclusively, by invoking the
new
operator, as in your example - then autoloading is a process that can kick in with an algorithm for loading the class. This process typically involves starting from the name of the missing class and producing a collection of possible pathnames - either absolute in the filesystem or relative to entries in theinclude_path
- from the name of the missing class and then doing performing aninclude
in the hopes that the class definition resides in one of them.In your case, you have called for a class named "Test". The file containing this class resides in the file
APPLICATION_PATH . 'tests/Test.php'
. And you have placed the directoryAPPLICATION_PATH . 'tests'
in theinclude_path
. But until there is an autoloading algorithm in place, there is no way for the system to connect the class name'Test'
to the file name'Test.php'
.The PEAR-style class name convention would provide this connection. And an autoloader that employs that convention - like the default Zend Framework autoloader - would be able to perform that mapping from class name to file name, and then
include
the required file.As a final complication, the conventional Zend Framework directory layout convention places certain classes in folders that are NOT on the include path: models, forms, services, controllers, view helpers, action helpers, etc. That's why you will often see more configuration of the autoloader - typically in the
Bootstrap
class - defining a mapping between certain classnames and certain places in the filesystem that are off theinclude_path
.For example, a class named 'Default_Model_User' might - at first glance - be expected to reside on the
include_path
in a file'Default/Model/User.php'
. But the standard app directory structure wants to place that file in 'application/models/User.php'. Note the plural 'models' and the lowercase 'm' in the path name, as well as the presence of "Default" in the classname. Extra config on the autoloader is required in order to make sense of that, the provide a pattern for the classname-to-filename mapping. This is usually accomplished with a resource autoloader.Hope any of this helps, either you or someone else. Cheers!
包含路径显示 PHP 将在何处查找它将要包含的脚本 - 因此,当您说
include('blah.php')
PHP 引擎将查找blah.php
在包含路径中的所有路径中。自动加载是一种功能,当 PHP 检测到需要该类但未定义该类时,允许调用某些函数。在这个函数中,您可以使用包含路径来查找包含该类的文件并加载它,但这些是完全不同的事情。
realpath() 与其中任何一个都不相关,它是一个返回给定文件的唯一文件系统路径的函数,其中包含诸如
..
和已解析的符号链接之类的内容。Include path shows where PHP would look for scripts that it is about to include - so that when you say
include('blah.php')
PHP engine would look forblah.php
in all paths in the include path.Autoloading is the functionality that allows certain function to be called when PHP detects that class is needed but not defined. In this function, you can use the include path to find the file containing the class and load it, but these are absolutely different things.
realpath()
is not related to any of those and is a function that returns unique filesystem path of a given file, with things like..
and symbolic links resolved.