PHP Syntax Statements Script Inclusion

 

- Info:

- Script Inclusion statements allow you to include content of another single PHP script.

PHP script which is being included must have proper PHP tags, like <?php ... ?>, which will not be copied.

- Result of script inclusion statements is one huge PHP script in which content of other PHP scripts was copied.

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

Before the script 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

 

- Script Referencing:

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

 

- Script Locating:

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