2

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

Folder structure

  • '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
Barmar
  • 741,623
  • 53
  • 500
  • 612
KevN
  • 33
  • 7

3 Answers3

4

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

Arshi
  • 501
  • 3
  • 13
2

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';
}
axiac
  • 68,258
  • 9
  • 99
  • 134
  • Thank you. Exactly what I'm considering. But the compiler out put an error that file not exist. Hmm. – KevN Jan 10 '16 at 11:03
0

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

Community
  • 1
  • 1
Clay
  • 4,700
  • 3
  • 33
  • 49