I've got a file that includes various php files with a rewrite framework. I'm importing a class and registering it, and it functions for all code on that page. However when I try to require() a file, it does not seem to have access to the class.
require __DIR__.'/lib/UserApp/Autoloader.php'; // Include UserApp Library
require __DIR__.'/lib/UserAppWidget/Autoloader.php'; // Include UserApp Widget, friendly-ifying API
UserApp\Autoloader::register();
UserApp\Widget\Autoloader::register();
use \UserApp\Widget\User;
User::setAppId("XXXXXX");
...
// Post for login
$router->post('/login', function() {
if(!User::authenticated()){
require('db_handlers/login.php');
}else{
header("Location: dashboard");
}
});
The code to check if the user is logged in correctly works, but the included file references the same class, and I get the following error: Fatal error: Class 'User' not found in /var/www/db_handlers/login.php on line 11
if(User::login($email, $password)){
if (isset($_SESSION['request'])) {
$request = $_SESSION['request'];
header("Location: $request");
unset($_SESSION['request']);
}else{
...
I thought that when code was included, it was as if the code was on the page in the first place. Why then, does the class not work?