Fatal error: Call to a member function ...on string

Fatal error: Call to a member function ...on string

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

Stefan Bogdanescu

Founder & Senior Architect

Fatal error: Call to a member function ...on string

When you encounter the PHP error Fatal error: Call to a member function 'property' on string, it usually points to a fundamental misunderstanding of how variables are being passed between objects or methods. In your specific case, the crash happens because you are trying to call $idea->checkIdea($title) where $idea is an instance of your ideas class, but that variable contains a string, not an object.

The root cause lies in how you are instantiating your classes and passing dependencies. Let's break down the execution flow based on the code provided.

In your main script, you initialize the connection correctly:

$conn = new connection();
$user = new users($conn->getConn());
$idea = new ideas($conn->getConn());

Here, $idea is clearly an instance of the ideas class. However, look closely at where the error occurs inside your checkIdea logic:

if (isset($_POST['addidea'])) {
    $title = mysql_real_escape_string($_POST['title']);
    $idea = mysql_real_escape_string($_POST['idea']); // <-- The problem starts here
    
    $check = $idea->checkIdea($title); 
}

On the line $idea = mysql_real_escape_string($_POST['idea']);, you are overwriting the object instance of $idea with a string. $_POST['idea'] is the content of your form input (e.g., "My new project idea"), which is naturally a string. When you run that function, PHP converts the input into a string and assigns it to the variable $idea.

Immediately after this line, when you execute $idea->checkIdea($title), PHP looks for the ideas class methods on the variable $idea. Since $idea is now just a plain text string, it does not have any methods. Attempting to access a method on a non-object triggers the fatal error.

The Fix: Scope and Naming

The immediate fix is simple: stop reusing the name $idea for two completely different things in the same scope. You are using the class instance to define the variable, but then you use the form input value to redefine it.

Rename your input variables to something distinct, such as $body or $content, to avoid shadowing your object:

if (isset($_POST['addidea'])) {
    $title = mysql_real_escape_string($_POST['title']);
    $body = mysql_real_escape_string($_POST['idea']); // Renamed to avoid conflict
    
    // Assuming $idea is still the object from earlier
    $check = $idea->checkIdea($title); 
    
    if ($check->num_rows == 0) {
        echo $idea->getUserId($_SESSION['username']);
    } else {
        echo "Sorry that idea title is already taken, please use another!";
    }
}

A Note on Security and Modern PHP

While fixing the variable scope issue resolves the crash, your current implementation relies on mysql_real_escape_string and direct SQL queries. This approach is considered legacy in the modern PHP ecosystem. The mysqli_* functions with escape strings are prone to edge cases where the delimiter matches the string content or when dealing with binary data.

For new projects, LaravelCompany recommends using PDO with prepared statements. Prepared statements separate the SQL logic from the user input entirely at the database level, making them immune to injection attacks without needing manual escaping. If you are maintaining an older codebase, stick to mysqli but ensure you understand that mysql_real_escape_string is deprecated in newer PHP versions and should be replaced with prepared statements eventually.

Dependency Injection Patterns

Your current pattern of instantiating the connection inside every controller or handler ($conn = new connection()) creates tight coupling. If you need to switch from MySQL to a different database driver later, every file that includes connection.php will break because it expects the specific mysqli class structure.

A more robust architecture involves creating a dedicated database manager class that handles the connection lifecycle centrally. This ensures that your data access logic remains agnostic to the underlying storage mechanism. By passing a single, well-tested connection object into your model classes (like your ideas class), you maintain separation of concerns while avoiding the need to re-instantiate connections repeatedly.

Our network