Källa: Code tutor | php.net |
When you include a file inside a parent file, the scope of variables defined in the included file will be the same as the variable scope at that particular line. However, any functions and classes that you have defined in the included file will have a global scope. Let’s understand it through an example
- create file inc.php
- Define to include ”include.php in new file
<?php
require_once('inc.php');
?>
Path
Always use __DIR__ to define path relative to your current __FILE__.
(Or another setting that is originally based on __DIR__/__FILE__)
One of my typical example is:
<?php
define(’__ROOT__’, dirname(dirname(__FILE__)));
require_once(__ROOT__.’/config.php’);
?>
instead of:
<?php require_once(’/var/www/public_html/config.php’); ?>