PHP call Class method / function

PHP call Class method / function

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

Stefan Bogdanescu

Founder & Senior Architect

Invoking Class Methods Safely in Legacy PHP Code

You are looking to call a specific class method based on parameters retrieved directly from $_GET. The snippet provided defines a Functions class with a static or instance-based filter method intended to sanitize user input. However, the code presented contains several critical vulnerabilities and deprecated practices that must be addressed before this logic can be safely invoked in any production environment.

The most immediate issue is the use of mysql_real_escape_string. This function relies on the mysql_* extension, which was deprecated in PHP 5.5 and removed entirely in PHP 7.0. Attempting to invoke this method in a modern PHP version will result in a fatal error or a warning, depending on your configuration. Furthermore, relying on get_magic_quotes_gpc() is obsolete; magic quotes were automatically disabled by default in modern PHP distributions years ago, making that conditional check unnecessary and confusing for future maintainers.

To correctly call this method, you must first instantiate the class. The provided code uses $var = filter($_GET['params']);, which implies a global function or a static call without an explicit object reference. If filter is defined inside the Functions class as shown, it cannot be accessed globally unless the class has public static properties or the method itself is declared static.

Here is how you would properly instantiate and invoke the method in a modern context:

class Functions {
    // Removed deprecated mysql_real_escape_string logic for safety
    public function filter($data) {
        $data = trim(htmlentities(strip_tags($data)));
        
        // Note: magic_quotes_gpc is obsolete, so this check is removed.
        // Also note: mysql_real_escape_string is deprecated and removed in PHP 7+.
        
        return $data;
    }
}

// Correct invocation pattern
$functions = new Functions();
$cleanData = $functions->filter($_GET['params']);

However, invoking a method to perform SQL escaping on a string retrieved from $_GET is an architectural anti-pattern. The filter method mixes input sanitization with database-specific escaping. In modern PHP development, you should never trust that mysql_real_escape_string is sufficient or even available. Instead, you should utilize Prepared Statements with PDO or MySQLi. This approach separates data cleaning from data querying, preventing SQL injection regardless of how the string is escaped later.

If your goal is simply to clean user input for display purposes (e.g., echoing it back in HTML), strip_tags() combined with htmlspecialchars() is the robust standard. htmlspecialchars converts special characters like <, >, and & into their HTML entities, which prevents XSS attacks without altering the data structure required for a database query.

Consider rewriting the logic to handle these cases distinctly:

// For display in HTML
$displaySafe = htmlspecialchars($input, ENT_QUOTES, 'UTF-8');

// For database insertion (using PDO)
$stmt = $pdo->prepare('INSERT INTO table (column) VALUES (:value)');
$stmt->execute(['value' => $pdoInput]); // Input goes directly to prepared statement

The original filter method attempts to do too much: it trims whitespace, removes HTML tags, decodes entities, and then escapes for SQL. This creates a dependency on the specific database driver and obscures the intent of the code. If you must maintain this legacy class for compatibility with very old systems (PHP 5.x), ensure you are running a version that still supports the mysql_* functions, though this is strongly discouraged.

For any new development or refactoring efforts, leveraging a framework like Laravel simplifies these concerns significantly. The framework provides robust helper functions and facades that handle input sanitization and database interactions securely out of the box. You can find more resources on modernizing PHP applications and utilizing secure patterns at https://laravelcompany.com.

Ultimately, calling a class method is straightforward syntax ($object->method()), but the logic inside that method dictates the safety of your application. Always prefer prepared statements over string escaping functions, and avoid mixing input validation with output encoding in the same utility function.

Our network