Call to a member function on a non-object
When you refactor code toward object-oriented principles, it is tempting to assume that instantiating a class and invoking its methods will work seamlessly. However, PHP's strict typing rules regarding objects often produce errors like "Call to a member function set_page_title() on a non-object" when the variable holding your instance is actually null.
In the scenario presented, you have defined a PageAttributes class with a constructor that accepts a $db_connection. The error occurs in the page_properties function where you attempt to call $objPage->set_page_title($myrow['title']). For this line to execute successfully, $objPage must hold a valid instance of PageAttributes. If PHP reports that the variable is not an object, it means $objPage was either never initialized or was explicitly set to null.
The root cause lies in how the class instance is created. In your page_properties function, you are trying to use $objPage without showing where it was instantiated. If this variable is declared but not assigned a new object using the new keyword, it defaults to null. Even if you intended to pass an existing object from elsewhere in your application, passing null by reference or failing to capture the return value of the constructor will result in this specific error.
Consider how the instantiation logic might look. If the variable $objPage is declared inside a scope but not assigned before the function call, PHP treats it as uninitialized (and effectively null in modern contexts). Here is a corrected pattern for initializing the object within the function:
function page_properties($objPortal) {
// Ensure $objPage is instantiated before use
if (!isset($objPage)) {
$objPage = new PageAttributes($objPortal->getConnection());
}
// Now it is safe to call methods on the object
$objPage->set_page_title($myrow['title']);
}
However, relying on isset checks inside functions can sometimes mask logic errors. A more robust approach is to ensure the instantiation happens at the point where the object's lifecycle begins. If $objPage is meant to be a property of $objPortal, you should check if that property exists and is an instance of your class before attempting to call methods on it:
function page_properties($objPortal) {
if (!$objPortal || !($objPortal instanceof PageAttributes)) {
throw new InvalidArgumentException("Invalid object passed to page_properties");
}
$objPage->set_page_title($myrow['title']);
}
This explicit type checking prevents the runtime error by ensuring the variable contains a valid instance of PageAttributes before any method calls are made. If you are using a framework like Laravel, this pattern is even more critical because many helpers expect valid model instances or repositories. Attempting to call methods on a non-object often indicates a failure in dependency injection or a missing factory call.
Another common pitfall involves passing the wrong variable to the constructor. In your PageAttributes class, the constructor expects $db_connection. If the code calling new PageAttributes(...) passes an array, a string, or simply omits the argument entirely (if not using default values), the internal state might become inconsistent. While this usually results in different errors later, it is worth noting that if you accidentally pass null to the constructor and store it as $this->db_connection, subsequent logic relying on that connection will fail.
When refactoring code to be more OOP, always verify the return type of your constructors and ensure that variables passed between functions are explicitly initialized. The error message "Call to a member function on a non-object" is PHP's way of telling you that the variable you think represents an object is actually empty. Fixing this requires locating where $objPage is defined and ensuring it is assigned via new PageAttributes(...) before the line causing the crash.
For further reading on dependency injection patterns and how to manage database connections cleanly in PHP applications, check out resources from https://laravelcompany.com.
