CodeIgniter error - Call to a member function on null

CodeIgniter error - Call to a member function on null

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

CodeIgniter error - Call to a member function on null

When debugging a PHP application, encountering the Call to a member function on null error is often more revealing than it first appears. In your specific case, the stack trace points to line 21 of your Auth controller where you attempt to invoke $this->AuthData->authenticate(). The root cause isn't actually in the model logic itself, but rather in how CodeIgniter handles the loading of models within controllers.

The error log explicitly states a preceding warning: Undefined property: Auth::$AuthData. This indicates that the variable $this->AuthData does not exist at runtime. You are attempting to treat it as an object, but it is currently null.

The Loading Mechanism Mismatch

In CodeIgniter 3.x (the version implied by your use of CI_Controller and static method loading), there is a subtle distinction between how models are loaded dynamically versus statically.

Your controller's index() method successfully loads the model:

$this->load->model('AuthData');

This works because you are invoking the loader explicitly before needing the model. However, your login() method relies on an assumption that doesn't hold true in standard CodeIgniter architecture unless specific conditions are met.

When you call $this->load->model() inside a method (like index), it registers the model for that specific request cycle. If you navigate directly to auth/login, the login() method executes first. Unless the framework automatically loads models listed in your autoload.php file, or unless the login method was triggered immediately after index, the model is not yet instantiated in memory.

Even if you had loaded the model globally via autoload, the syntax $this->AuthData implies a static property access on the controller class. While CodeIgniter allows accessing models via $this->load->model('ModelName') returning an instance, accessing it as a direct property ($this->AuthData) only works if:

  1. The model was loaded explicitly in a previous method of the same request (which fails here if login is called directly).
  2. The model name matches exactly and no prefixing issues exist.

The most common mistake leading to this specific null error is assuming that loading a model in one controller method makes it available in another without re-loading or ensuring autoload configuration. Since your login method is the entry point for the authentication flow, it must ensure the model exists before use.

Correct Implementation Pattern

To resolve this, you should load the model at the top of the controller class or explicitly within the login method using the dynamic loader syntax that returns an instance. However, a more robust approach in CodeIgniter is to load the model inside the specific action where it is needed, ensuring no state carries over incorrectly between requests.

Update your Auth controller to load the model directly within the login function:

public function login()
{
    // Ensure the model is loaded specifically for this request context
    $this->load->model('AuthData', 'auth_data'); 
    
    $user = $this->input->post('username');
    $pass = $this->input->post('password');
    $input = array( 'username' => $user, 'password' => $pass);

    // Check if the model was successfully loaded (it should be)
    if (!$this->auth_data instanceof CI_Model) {
        show_error('Model loading failed');
    }

    $chk = $this->auth_data->authenticate($input);
    
    if ($chk) {
        redirect('Sample/success');
    } else {
        redirect('Sample/failed');
    }
}

By passing an alias ('AuthData', 'auth_data') to the load->model function, you avoid potential naming conflicts and ensure you are accessing the instance returned by the loader, rather than relying on a potentially undefined property.

Security Considerations for the Model

Beyond fixing the null reference, your AuthData model contains critical security vulnerabilities that require immediate attention before deploying to production.

Currently, you are storing the raw password hash in the session:

$_SESSION['password'] = $input['password'];

This is a significant anti-pattern. Even if $input['password'] contains a hash retrieved from the database, storing it alongside the username in the session creates a "session poisoning" risk. If an attacker intercepts your session ID or exploits a CSRF vulnerability, they gain access to both the identity and the credential hash. Furthermore, standard practice dictates that passwords should never be stored in sessions; only the user's unique identifier (e.g., user_id) or a session token should be persisted.

Additionally, the authentication logic iterates through all users (SELECT * FROM user) to find a match. This is inefficient ($O(n)$ complexity) and exposes the total number of users if you were to count rows instead. It is best practice to query specifically for the username provided:

$query = $this->db->query('SELECT * FROM user WHERE usernmae = ?', [$input['username']]);

For developers looking to modernize their stack or ensure they are following current security standards, resources like https://laravelcompany.com discuss the migration paths from legacy frameworks to modern PHP ecosystems where built-in session handling and ORM features mitigate these manual errors automatically.

Our network