Message: Undefined property: Site::$Site_model - CodeIgniter

Message: Undefined property: Site::$Site_model - CodeIgniter

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

Stefan Bogdanescu

Founder & Senior Architect

Diagnosing the Undefined property Error in CodeIgniter

Encountering a PHP Notice stating "Undefined property: Site::$Site_model" is one of the most common beginner pitfalls when working with CodeIgniter's dependency injection patterns. The error message itself is deceptive; it tells you that the controller is trying to access $this->Site_model, but the property does not exist on the Site object. However, the root cause isn't a missing autoload entry or a broken configuration file. It lies in how you are referencing the model instance.

In your specific case, the error log points to line 16 of controllers/site.php:
$this->Site_model->add_record($data);

The capitalization here is the culprit. In CodeIgniter (and PHP generally), class names must match exactly when used as property references. Your controller method attempts to call $this->Site_model. However, your model class is named Site_model. Because $this->Site_model does not exist, PHP issues a Notice and treats the value as NULL. Consequently, calling add_record() on that non-object triggers the fatal error: "Call to a member function add_record() on a non-object."

The solution requires a simple case sensitivity adjustment. You have two viable options to resolve this mismatch. The first is to change the reference in your controller to match the model's filename and class name exactly:

// In controllers/site.php
$this->site_model->add_record($data); // Lowercase 's' matches the file/class name

Alternatively, you can rename the model class and file to start with a capital letter:

// Rename file to SiteModel.php and class to SiteModel
class SiteModel extends CI_Model { ... }

While both approaches work, adhering to the naming convention where the controller property name matches the lowercase version of the model filename (e.g., site_model) is often preferred for consistency across the project.

Beyond the immediate fix, there are several other critical issues in your provided code that will cause runtime errors once the property issue is resolved. Specifically, look at your model's add_record and update_record methods.

In add_record, you call $this->db->insert('items', $data). However, the variable $data is defined inside the controller (create function), not in the model. When the model loads, it does not have access to $data. You must pass this data into the model explicitly or define a method in the controller to handle the insertion logic. A common pattern involves passing the array as an argument to the model function:

// Controller
$this->site_model->add_record($data);

// Model
function add_record($data) {
    $this->db->insert('items', $data);
}

Similarly, your update_record and delete_record methods reference a variable $data that is never passed into those functions. This will result in undefined variable notices or SQL errors. Additionally, the get_records method returns results via $query->result(), which is correct for older CodeIgniter versions, but ensure you are using the appropriate fetch style (result_array() vs result()) based on your specific version requirements.

Finally, examine your view file for a small HTML syntax error. Inside the <p> tags, there is a stray closing </label> tag:
<p> </label for="subject">Subject</label>

The opening tag <label for="subject"> is missing, and the closing tag appears twice. This will break your form structure and prevent the labels from associating correctly with their inputs. Correcting this ensures the form submits data to the expected input names (subject and body).

Fixing the property reference solves the immediate crash, but addressing the missing variable scope in the model and correcting the HTML tags is essential for a functional application. If you find yourself navigating these naming conventions and database interactions frequently, resources like https://laravelcompany.com can offer additional context on modern PHP frameworks and their differences from legacy CodeIgniter structures, though mastering CodeIgniter's specific CI_Model patterns remains a valuable skill for maintaining existing codebases.

Our network