Architect
Architect

Reputation: 103

Using a class to include / require a file

I'm using the following code almost everytime that I require a file:

$file = 'my_file.php';
if (!file_exists ($file)) {
    sessionLog(basename($_SERVER['PHP_SELF']).">Class NOT found: ".$file);
}else{
    require ($file);
    sessionLog(basename($_SERVER['PHP_SELF']).">Class Loaded: ".$file);
}

Is there a practical way to create a class or function to avoid re-writing the code every time I require a file?

// Function to validate and include file

include_this_file(my_file.php)

Upvotes: 0

Views: 56

Answers (1)

T0xicCode
T0xicCode

Reputation: 4951

The standard nowadays it to use autoloading to manage most (if not all) of the file, class and functions.

Projects using composer benefit from it's PSR-compatible implementation of autoloading, but you can also implement your own and register it with the PHP interpreter.

Upvotes: 1

Related Questions