Created
September 17, 2018 17:34
-
-
Save shohel/d04d3d27d6e15e551f2eff9b0eb435ce to your computer and use it in GitHub Desktop.
PHP file include by dot notation tricks for developers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Dot notation Directory access / file include | |
*/ | |
/** | |
* @param string $file_name | |
* | |
* Without Given file extension, it will be add from function | |
* | |
* for @usage 1 | |
*/ | |
function load_file($file_name = ''){ | |
$ext = substr($file_name, strpos($file_name, '.', -4) ); | |
$file_base_name = substr($file_name, 0, strpos($file_name, '.', -4) ); | |
$file_name = str_replace('.', DIRECTORY_SEPARATOR, $file_base_name).$ext; | |
if (file_exists($file_name)){ | |
include $file_name; | |
} | |
} | |
/** | |
* @param string $file_name | |
* | |
* Without Given file extention, it will add from function | |
* | |
* for @usage 2 | |
*/ | |
function load_file($file_name = ''){ | |
$file_name = str_replace('.', DIRECTORY_SEPARATOR, $file_name).'.php'; | |
if (file_exists($file_name)){ | |
include $file_name; | |
} | |
} | |
/** | |
* @param string $file_name | |
* @return string | |
* | |
* Just return the file path | |
* for @usage 3 | |
*/ | |
function get_file($file_name = ''){ | |
return str_replace('.', DIRECTORY_SEPARATOR, $file_name).'.php'; | |
} | |
/** | |
* Usage with file extension | |
* | |
* @usage 1 | |
*/ | |
load_file('directory.directory1.directory2.filename.php'); | |
//include file from (directory/directory1/directory2/filename.php) | |
/** | |
* Usage without file extension | |
* @usage 2 | |
*/ | |
load_file('directory.directory1.directory2.filename'); | |
//include file from (directory/directory1/directory2/filename.php) | |
/** | |
* Usage via return method | |
* @usage 3 | |
*/ | |
$file_path = get_file('directory.directory1.directory2.filename'); | |
//return file path (directory/directory1/directory2/filename.php) | |
//Do whatever you want with $file_path |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment