Reputation: 23
according to the php manual:
Specifies a list of directories where the require, include, fopen(), file(), readfile() and file_get_contents() functions look for files. The format is like the system's PATH environment variable: a list of directories separated with a colon in Unix or semicolon in Windows.
i dont understand why you should specify the directory ?
for example i saw this code
<?php
defined("DS") || define("DS", DIRECTORY_SEPERATOR);
defined("ROOT_DIR") || define("ROOT_DIR", realpath(dirname(__FILE__).DS."..".DS));
defined("CLASSES_DIR") || define("CLASSES_DIR", "classes");
set_include_path(implode(PATH_SEPERATOR, array(
realpath(ROOT_DIR.DS.CLASSES_DIR.DS),
get_real_path()
)));
?>
why add the CLASSES_DIR to the include path ? cant you already do something like require("classes/example.php") ?
Upvotes: 2
Views: 678
Reputation: 1213
By setting the include path you do not need to explicitly specify the full path to your files anymore since php then has some options where to look after i.e. your 'example.php'. Assuming you had a php file called 'exampleClass.php' inside your 'DOCUMENT_ROOT/classes' directory and an index.php located directly under your DOCUMENT_ROOT:
<?php
set_include_path(DOCUMENT_ROOT . DIRECTORY_SEPARATOR . 'classes');
// Note that exampleClass.php is not located in the same directory as this index.php
require('exampleClass.php');
?>
But I would like to add that in the depicted example there are probably better alternatives of organizing and structuring your project to avoid 'require(/hope/this/path/is/the/right/one.php)' statements. E.g. by using namespaces in combination with autoloading
Upvotes: 2