php - 定义一个包含文件的目录
问题描述
我目前正在从事我的第一个独立项目,我希望将我的代码保持在尽可能好的标准,我一直在查看 PHP 手册,但我似乎缺少如何干净地定义要包含的目录,因为例如我的模板文件夹是“模板/默认”,我想定义一次。
TEMPDIR = templates/default
require_once (__DIR__ . '/TEMPDIR/header.php');
如果有人能指导我正确的方向,将不胜感激,谢谢
解决方案
欢迎来到堆栈。
我猜你想定义一个常量TEMPDIR
,然后在require_once()
.
要定义变量,请使用define()
(请参阅手册https://secure.php.net/manual/en/language.constants.php):
<?php define('TEMPDIR', 'templates/default');
要在中使用常量,require_once()
您必须进行字符串连接(请参阅手册http://php.net/manual/en/language.types.string.php#language.types.string.useful-funcs):
<?php require_once (__DIR__ . '/' . TEMPDIR . '/header.php');
您还应该考虑使用DIRECTORY_SEPARATOR
常量来确保您使用适用于任何操作系统的分隔符(请参阅手册http://php.net/manual/en/dir.constants.php):
<?php require_once (__DIR__ . DIRECTORY_SEPARATOR . TEMPDIR . DIRECTORY_SEPARATOR 'header.php');
干杯!
推荐阅读
- r - 如何将日期时间转换为 POSIXct 或将数字/聚合时间序列间隔转换为每小时间隔?
- java - Spring Boot - 外部 tomcat 服务器上的 JDBC 连接问题
- reactjs - 使用 ReactJs 和 mobx 过滤搜索
- python - 如何对 django get_context_data() 中的 2 列求和
- vba - 访问所需的关键参考
- python - 具有列表值的字典作为多索引数据框
- javascript - 如何使用 Excel vba 从 javascript/CSS 网站中选择元素?
- java - 在 kstreams 应用程序中转换消息的时间出乎意料地长
- phabricator - Phabricator Conduit API 端点“diffusion.lastmodifiedquery”中“路径”字段的示例输入
- python - index() 函数在我的代码中表现得很奇怪