可能重复:
PHP 需要来自顶层目录的文件
我已经使用 PHP 很长时间了,但我一直只是为了完成工作而编写它,现在我正在努力学习最佳实践并提高我的技能。现在我想知道从任何目录将文件包含在 PHP 脚本中的最佳方法是什么。
我一直在做以下事情:
<?php
// From settings.php
define('ABSPATH', '/home/dalyn/public_html/');
// Then I put this at he top of all of my other files
include 'includes/settings.php';
include ABSPATH . 'includes/db.php';
include ABSPATH . 'includes/functions.php';
?>
但是,如果我的文件位于子目录中,我需要执行以下操作:
<?php
include '../includes/settings.php';
include ABSPATH . 'includes/db.php';
include ABSPATH . 'includes/functions.php';
?>
有没有更好的方法来做到这一点?我开始学习 OOP,所以如果有适用的 OOP 解决方案,我会全力以赴。
谢谢你。