·  PHP – Syntax – Keywords – include(), require(), require_once()

 

- Info:

  - include(), require() and require_once() statements are simply replaced by content of specified PHP file.

  - Result is one huge PHP file in which every other needed PHP file was copied.

  - Each PHP file might have it's own namespace which has nothing to do with the name of the file or its actual location.

    Before the file is copied, namespace is used to create full class names and only then the file is copied.

  - This tutorial is based on:

    - http://www.php.net/manual/en/function.include.php

    - http://php.net/manual/en/function.require.php

    - http://php.net/manual/en/function.require-once.php

    - http://php.net/manual/en/language.namespaces.basics.php

 

- include():

  - include() is replaced by content of specified PHP file even if file was already copied.

  - If it fails it produces E_WARNING and continues the script.

 

- require():

  - require() is replaced by content of specified PHP file even if file was already copied.

  - If it fails it produces fatal E_COMPILE_ERROR and stop the script.

 

- require_once():

  - require_once() is replaced by content of specified PHP only if file was not already copied.

  - If it fails it produces fatal E_COMPILE_ERROR and stop the script.

 

- Referencing file:

  - File can be referenced in three different ways:

    - By filename in which case it will be looked for in each of the directories listed in include_path:

      require_once 'MyClass.php';

    - By relative path in which case it will be looked for relativly to each of the directories listed in include_path:

      require_once 'Vehicles\MyClass.php';    //In subdirectory Vehicles

      require_once '..\Vehicles\MyClass.php'; //In directory Vehicles in superdirectory

    - By absolute path in which case it will be looked for only in that specific location:

      require_once 'C:\Vehicles\MyClass.php';

  - PHP will use the first file that it finds checking include_path directories from left to right:

    include_path=".;..;Plains\Engines;D:\Installed\Programming\PHP533\pear"

  - If file is not found PHP will check once more positioning itself at the current directory ".".

    Current directory "." is directory which contains PHP file where include/require statement was used.

 

- Procedure:

  - Create PHP Console Application C:\Temp\PHP\Test.php

  - Create C:\Temp\PHP\Vehicles\MyClass.php

 

    Test.php

    <?php

      require_once 'Vehicles\MyClass.php';

    //require      'Vehicles\MyClass.php';

    //include      'Vehicles\MyClass.php';

      MyClass::displayString();

    ?>

 

    MyClass.php

    <?php

   

      class MyClass {

      

        public $var = "Hello from object!\n";

      

        public function displayVar() {

          echo($this->var);

        }

        

        public function displayString() {

          echo("Hello from classs!\n");

        }   

          

      }

      

    ?>