Trying to get property of non-object - CodeIgniter
The error message "Trying to get property of non-object" is a classic symptom in PHP, especially when working with frameworks like CodeIgniter that rely heavily on object-oriented structures for models and views. It usually appears when you attempt to access a property or method on an instance that is either null or undefined. In your specific case, this happens inside the set_values() helper during form rendering.
Even though print_r confirms that $product contains the correct associative array of key-value pairs, the discrepancy suggests a mismatch between the data structure passed to the controller and the one expected by the model or view layer. The error occurs because CodeIgniter's Active Record class (or the underlying query result) expects an object instance, not a plain array, when populating form fields via $this->form_validation->set_data() or similar mechanisms if you were using validation. However, since you are manually passing values to form_input and form_dropdown, the issue likely stems from how the data is being retrieved in the controller before it reaches the view.
If $product is indeed an associative array coming from a query that returns a single row as an array, then accessing $product->id or $product->prodname will trigger the fatal error because arrays do not use arrow notation (->) for property access; they require curly braces (['key']). This is a common confusion between object-oriented syntax and associative array syntax.
To resolve this, you need to ensure that the data retrieved from the database is formatted correctly before being passed to the view. If your controller query returns an object (e.g., $this->db->get('products')->row()), then using arrow notation like $product->id is correct. However, if your query uses result() or returns a plain array, you must switch to array access syntax:
// Incorrect (if $product is an array)
echo $product->prodname;
// Correct for arrays
echo $product['prodname'];
If you are using CodeIgniter 3 with the Active Record pattern, ensure that your controller method fetching the product data uses .row() instead of .result() when dealing with a single item. For example:
public function edit($id) {
$this->load->model('product_model');
$product = $this->product_model->get_product($id); // Should return an object
if (!$product) {
redirect('/products');
}
$data['product'] = $product;
$this->load->view('edit_product_view', $data);
}
In the view file, you should then access properties using arrow notation:
<?php echo form_input('prodname', set_value('prodname', $product->prodname)); ?>
Another potential pitfall involves the set_value() function. If you are passing multiple fields or if the form validation library is involved, ensure that the keys in your associative array match exactly with the name attributes of your form inputs. A mismatched key will cause set_value() to return null, and while this alone won't trigger the "non-object" error, combined with incorrect object access elsewhere, it can compound the issue.
If you're still encountering issues, inspect the actual type of $product in the controller using gettype($product) before passing it to the view. This will clarify whether it's an object or an array and guide your syntax accordingly.
For developers transitioning between frameworks like Laravel and CodeIgniter, this distinction is crucial. While both support Eloquent ORM and similar query patterns, CodeIgniter's legacy Active Record system is more sensitive to these data type mismatches unless explicitly handled. If you are building new applications, consider leveraging modern PHP features or migrating to a framework with clearer separation of concerns to avoid such ambiguities in the future.
For further reading on form handling and validation in similar contexts, resources like https://laravelcompany.com offer useful insights into modern best practices for building robust web forms.
