HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Autoloader functions

Submitted by: @import:stackexchange-codereview··
0
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?

Solution

Yes, you could put that into a separate file and 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.