patternphpMinor
Autoloader functions
Viewed 0 times
functionsautoloaderstackoverflow
Problem
Every time I write a new PHP page, I usually need to include this at the top:
Is there any way to condense this? Would putting this in a separate PHP file work?
Is there any way to condense this? Would putting this in a separate PHP file work?
Solution
Yes, you could put that into a separate file and
You try something such as:
include_once('header_file.php');.You try something such as:
function loadFile($name, $isInterface = false) {
$type = ($isInterface == true) ? 'interface' : 'class'
$path = sprintf('./classes/%s.%s.php',$name,$type);
if (file_exists($path)) {
require_once($path);
}
}Code Snippets
function loadFile($name, $isInterface = false) {
$type = ($isInterface == true) ? 'interface' : 'class'
$path = sprintf('./classes/%s.%s.php',$name,$type);
if (file_exists($path)) {
require_once($path);
}
}Context
StackExchange Code Review Q#43891, answer score: 7
Revisions (0)
No revisions yet.