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


use \UserApp\Widget\User;



// Post for login
$router->post('/login', function() {
        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");

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?

Recommended Answers

All 2 Replies

Honestly, I don't see any reasons why it shouldn't work? All static method can be use by other classes. Except, if this is a separate controller file /login . That would change the application's loaded controller.

It is pretty much equivalent to

<form method="post" action="/login">

On form submit, it would take you to /application_directory/login_Controller/Method/. In your case you are loading the login controller instance. If the login controller page does not require the User class, then User::login() method cannot be executed.

I wrote an application on CI where the user can login and will never leave the page. To do this, you make the method of the current controller page that will process the form.

So, for instance a URI appdirectory/login_controller/ can process the form by assigning the processing to another method called process. That would give us a new uri of /appdirectory/login_controller/process/. However, even after the form has been processed, the controller page did not change and we are still using the same controller. Only the method process is added.

If the login_controller file require the User class, then the static method of User should be available.

Example class uitilizing static method of another class: This is an example only. Your framework may have different ways of doing things.


class LoginController extends BaseController{

    public function __construct(){}

    public function index(){
        echo 'this is the default content on load';


    public function process(){
        echo 'This is the form processor';
        if(isset($email) AND (isset($password)){
            User::login($email, $password);


The form for the process method will be something like this

  <form action="/process" method="yourMethod">

On submit /process is added to the url. Ofte times, you will have to back it up by one slash like this ../process that would override the controller's slash..

Turns out I have to register() and use the class on every page it's included on.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.