【问题标题】:Using a class to include / require a file使用一个类来包含/需要一个文件
【发布时间】:2015-03-31 06:29:55
【问题描述】:

我几乎每次需要文件时都使用以下代码:

$file = 'my_file.php';
if (!file_exists ($file)) {
    sessionLog(basename($_SERVER['PHP_SELF']).">Class NOT found: ".$file);
}else{
    require ($file);
    sessionLog(basename($_SERVER['PHP_SELF']).">Class Loaded: ".$file);
}

有没有一种实用的方法来创建一个类或函数,以避免每次需要文件时都重新编写代码?

// Function to validate and include file

include_this_file(my_file.php)

【问题讨论】:

标签: php function class require


【解决方案1】:

现在的标准是使用autoloading 来管理大部分(如果不是全部)文件、类和函数。

使用 composer 的项目受益于 autoloading 的 PSR 兼容实现,但您也可以实现自己的并将其注册到 PHP 解释器。

【讨论】:

    猜你喜欢
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 2015-09-05
    • 2022-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多