检查是否定义了 VS PSR-4 class 自动加载。为什么在 PSR-4 中自动加载不需要 "system" contant 定义?
Checking if defined VS PSR-4 class autoload. Why in PSR-4 autoload need no "system" contant definition?
在 CMS 或任何已知的 php 系统中,遵循以下做法:
在 "main php" 文件中声明了一个常量,例如在 moodle 中定义了 MOODLE_INTERNAL
并且为了使用数据库,文档建议如下(如 documentation):
defined('MOODLE_INTERNAL') || die();
// Rest of code here
据我了解这种做法是用来检查php文件是否执行"in system".
虽然使用 PSR-4 自动加载的框架(例如 symfony)没有这样的要求,甚至也没有记录。因此我有这个紧迫的问题:
为什么在 PSR-4 autoload 中没有这样的要求来检查 php 文件是否被执行 "in system"?
我认为应该将此代码添加到只打算包含但从未调用过它们自己的文件中。然而在 PSR-4 中,包含的文件从不包含可执行代码,也不应该直接访问它们,这就是为什么不再需要这一行的原因。如果文件仅包含 class 定义,则执行此文件将不会执行任何操作。
在过去,您的代码可能跨越多个文件,并且所有代码都是以程序化风格编写的。如果有人直接访问此 PHP 文件,他们可能会收到错误或更糟的是破坏您的网站。
看看Joomla是怎么解释的:Why do most of the Joomla! PHP files start with defined(' JEXEC')?
_JEXEC
is a constant that is typically defined in the index.php
file at the root of the Joomla! instance and is used to mark a secure entry point into Joomla.
(...)
So the general rule for the JEXEC check is if the PHP file depends on another file to operate properly. Typically if you access a file directly without the JEXEC check and a PHP error is raised (presuming your PHP error reporting is set to show errors by default) about a missing variable, function, object or similar then the file needs to be protected. - https://docs.joomla.org/JEXEC
在 CMS 或任何已知的 php 系统中,遵循以下做法:
在 "main php" 文件中声明了一个常量,例如在 moodle 中定义了 MOODLE_INTERNAL
并且为了使用数据库,文档建议如下(如 documentation):
defined('MOODLE_INTERNAL') || die();
// Rest of code here
据我了解这种做法是用来检查php文件是否执行"in system".
虽然使用 PSR-4 自动加载的框架(例如 symfony)没有这样的要求,甚至也没有记录。因此我有这个紧迫的问题:
为什么在 PSR-4 autoload 中没有这样的要求来检查 php 文件是否被执行 "in system"?
我认为应该将此代码添加到只打算包含但从未调用过它们自己的文件中。然而在 PSR-4 中,包含的文件从不包含可执行代码,也不应该直接访问它们,这就是为什么不再需要这一行的原因。如果文件仅包含 class 定义,则执行此文件将不会执行任何操作。
在过去,您的代码可能跨越多个文件,并且所有代码都是以程序化风格编写的。如果有人直接访问此 PHP 文件,他们可能会收到错误或更糟的是破坏您的网站。
看看Joomla是怎么解释的:Why do most of the Joomla! PHP files start with defined(' JEXEC')?
_JEXEC
is a constant that is typically defined in theindex.php
file at the root of the Joomla! instance and is used to mark a secure entry point into Joomla.
(...)
So the general rule for the JEXEC check is if the PHP file depends on another file to operate properly. Typically if you access a file directly without the JEXEC check and a PHP error is raised (presuming your PHP error reporting is set to show errors by default) about a missing variable, function, object or similar then the file needs to be protected. - https://docs.joomla.org/JEXEC