在 php 中真正需要做什么
What does require really do in php
我是 PHP 的新手,在使用 require 时遇到问题。
- 'Config.php' 在父文件夹中
- 模型包含测试,其中'LaptopModel.php'是。
- 控制器包含文件'LaptopController.php'
在Config.php中:
<?php
$host = "localhost";
$username = "admin";
在LaptopModel.php中:
<?php
require '../../Config.php';
在LaptopController.php中:
<?php
require '../Model/test/LaptopModel.php';
问题是服务器处理此文件时 LaptopController.php 中的代码到底是什么?
我从两个方面考虑,是否会导致错误。
<?php
require '../../Config.php';// get the original code from required file
//this cause an error, because from LaptopController.php
//the link to Config.php must be '../Config.php'
或者它将是:
<?php
$host = "localhost";
$username = "admin";
//get what was required from Laptopmodel.php file, not the original code
关于你的问题部分,Require 真正做的很简单......
如果文件丢失或找不到它会产生一个致命错误并停止脚本的执行
通常,所有内容都与您最初加载的 PHP 文件的路径(您的工作目录)相关。 运行 命令 getcwd()
查看它试图从哪里加载文件。在这种情况下,它看起来像是基于 index.php
所在的目录。
getcwd — Gets the current working directory
关于include的信息(他们指的当前目录是当前工作目录):
If a path is defined — whether absolute (starting with a drive letter or \ on Windows, or / on Unix/Linux systems) or relative to the current directory (starting with . or ..) — the include_path will be ignored altogether. For example, if a filename begins with ../, the parser will look in the parent directory to find the requested file.
the question is what the code really is in LaptopController.php when the server process this file?
文件LaptopController.php
完全包含您在其中写入的内容。
PHP 解释器不会更改文件的内容,无论是在磁盘上还是在将文件加载到内存中时。
require
不像 copy/paste 那样工作。它更像是一个函数调用(但它不像函数那样引入局部作用域)。
编译 LaptopController.php
的代码时,语句 require '../Model/test/LaptopModel.php';
被转换为表示 "load and execute the code in this file".
的操作码
LaptopModel.php
中的代码在编译阶段未被读取和编译。它甚至不检查文件路径 (../Model/test/LaptopModel.php
) 是否存在。所有这些都发生在运行时,如果执行点到达require
语句。
您可以轻松验证这一点:将 require
语句放入 if
语句并确保所需文件有语法错误(或不存在)。如果执行包含 require
的 if
分支,则会触发致命错误并结束脚本。否则它会愉快地运行,因为 require
语句没有被执行。
// File 'a.php' doesn't exist
if (false) {
// This statement never runs, no error is triggered
require 'a.php';
}
我是 PHP 的新手,在使用 require 时遇到问题。
- 'Config.php' 在父文件夹中
- 模型包含测试,其中'LaptopModel.php'是。
- 控制器包含文件'LaptopController.php'
在Config.php中:
<?php
$host = "localhost";
$username = "admin";
在LaptopModel.php中:
<?php
require '../../Config.php';
在LaptopController.php中:
<?php
require '../Model/test/LaptopModel.php';
问题是服务器处理此文件时 LaptopController.php 中的代码到底是什么? 我从两个方面考虑,是否会导致错误。
<?php
require '../../Config.php';// get the original code from required file
//this cause an error, because from LaptopController.php
//the link to Config.php must be '../Config.php'
或者它将是:
<?php
$host = "localhost";
$username = "admin";
//get what was required from Laptopmodel.php file, not the original code
关于你的问题部分,Require 真正做的很简单...... 如果文件丢失或找不到它会产生一个致命错误并停止脚本的执行
通常,所有内容都与您最初加载的 PHP 文件的路径(您的工作目录)相关。 运行 命令 getcwd()
查看它试图从哪里加载文件。在这种情况下,它看起来像是基于 index.php
所在的目录。
getcwd — Gets the current working directory
关于include的信息(他们指的当前目录是当前工作目录):
If a path is defined — whether absolute (starting with a drive letter or \ on Windows, or / on Unix/Linux systems) or relative to the current directory (starting with . or ..) — the include_path will be ignored altogether. For example, if a filename begins with ../, the parser will look in the parent directory to find the requested file.
the question is what the code really is in LaptopController.php when the server process this file?
文件LaptopController.php
完全包含您在其中写入的内容。
PHP 解释器不会更改文件的内容,无论是在磁盘上还是在将文件加载到内存中时。
require
不像 copy/paste 那样工作。它更像是一个函数调用(但它不像函数那样引入局部作用域)。
编译 LaptopController.php
的代码时,语句 require '../Model/test/LaptopModel.php';
被转换为表示 "load and execute the code in this file".
LaptopModel.php
中的代码在编译阶段未被读取和编译。它甚至不检查文件路径 (../Model/test/LaptopModel.php
) 是否存在。所有这些都发生在运行时,如果执行点到达require
语句。
您可以轻松验证这一点:将 require
语句放入 if
语句并确保所需文件有语法错误(或不存在)。如果执行包含 require
的 if
分支,则会触发致命错误并结束脚本。否则它会愉快地运行,因为 require
语句没有被执行。
// File 'a.php' doesn't exist
if (false) {
// This statement never runs, no error is triggered
require 'a.php';
}