What does require really do in php

谁说我不能喝 提交于 2019-12-24 15:56:16

问题


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

  • 'Config.php' is in the parent folder
  • Model contains test, in which 'LaptopModel.php' is.
  • Controller contains the file 'LaptopController.php'

in Config.php:

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

in LaptopModel.php:

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

in LaptopController.php:

<?php
require '../Model/test/LaptopModel.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'

or it will be:

<?php
$host = "localhost";
$username = "admin";
//get what was required from Laptopmodel.php file, not the original code

回答1:


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

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 doesn't work like copy/paste. It works more like a function call (but it doesn't introduce a local scope as functions do).

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".

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.

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';
}



回答2:


Respective to you questions part what Require really do is simple... If file is missing or not found It will produce a Fatal Error and halt the execution of the script

Source : http://www.w3schools.com/php/php_includes.asp




回答3:


Generally, everything is relative to the path of your initially loaded PHP file (your working directory). Run the command getcwd() to see where it is attempting to the load files from. In this case, it looks like it includes based off the directory where index.php is.

getcwd — Gets the current working directory

Link http://php.net/getcwd

Info about include (the current directory they are referring to is the current working directory):

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.

Link http://php.net/manual/en/function.include.php



来源:https://stackoverflow.com/questions/34671230/what-does-require-really-do-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!