在PHP中真正需要做什么 [英] What does require really do in php

查看:87
本文介绍了在PHP中真正需要做什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP新手,在使用require时遇到问题.

I'm new to PHP and facing a problem when I use require.

  • "Config.php"位于父文件夹中
  • 模型包含测试,其中有"LaptopModel.php".
  • 控制器包含文件"LaptopController.php"

在Config.php中:

in Config.php:

<?php
$host = "localhost";
$username = "admin";

在LaptopModel.php中:

in LaptopModel.php:

<?php
require '../../Config.php';

在LaptopController.php中:

in LaptopController.php:

<?php
require '../Model/test/LaptopModel.php';

问题是服务器处理此文件时,LaptopController.php中的代码实际上是什么? 我用两种方法来考虑,这是否会导致错误.

the question is what the code really is in LaptopController.php when the server process this file? I think about that in 2 ways, that lead to error or not.

<?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

推荐答案

问题是服务器处理此文件时,LaptopController.php中的代码实际上是什么?

the question is what the code really is in LaptopController.php when the server process this file?

文件LaptopController.php完全包含您在其中写入的内容. PHP解释器不会更改文件的内容,无论是在磁盘上还是在将其加载到内存中时.

The file LaptopController.php contains exactly what you write in it. The PHP interpreter doesn't change the content of the file, neither on disk nor when it loads it in memory.

require 不能像复制/粘贴那样工作.它的工作方式更像一个函数调用(但它不会像函数那样引入局部作用域).

require doesn't work like copy/paste. It works more like a function call (but it doesn't introduce a local scope as functions do).

编译LaptopController.php的代码时,语句require '../Model/test/LaptopModel.php';转换为一个操作码,该操作码表示在此文件中加载并执行代码".

When the code of LaptopController.php is compiled, the statement require '../Model/test/LaptopModel.php'; is transformed into an opcode that says "load and execute the code in this file".

在编译阶段不会读取和编译LaptopModel.php中的代码.它甚至不检查文件(../Model/test/LaptopModel.php)的路径是否存在.所有这些都会在运行时发生,如果执行点到达require语句.

The code from LaptopModel.php is not read and compiled during the compilation phase. It doesn't even check if the path of the file (../Model/test/LaptopModel.php) exists. All these happen during runtime, if the execution point reaches the require statement.

您可以轻松地验证这一点:将require语句放入if语句中,并确保所需的文件存在语法错误(或不存在).如果执行包含requireif分支,则会触发致命错误,脚本结束.否则它会快乐地运行,因为require语句未执行.

You can verify this easily: place a require statement into an if statement and make sure the required file has a syntax error (or it doesn't exist). If the if branch that contains the require is executed then a fatal error is triggered and the script ends. Otherwise it runs happily because the require statement is not executed.

// File 'a.php' doesn't exist
if (false) {
    // This statement never runs, no error is triggered
    require 'a.php';
}

这篇关于在PHP中真正需要做什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆