How to fix error: Deprecated: Required parameter

How to fix error: Deprecated: Required parameter

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

Stefan Bogdanescu

Founder & Senior Architect

Fixing "Deprecated: Required Parameter" in PHP Functions

You are encountering a Deprecated warning because your function signature violates the strict ordering rules enforced by modern PHP versions regarding default parameters. The error message explicitly points to the line where the function is defined:

function getAllFrom($field, $table, $where = NULL, $and = NULL, $orderfield, $ordering = "DESC") {
    // ...
}

In this definition, you have optional parameters ($where and $and) followed immediately by a required parameter ($orderfield). When the caller invokes the function without providing an argument for $orderfield, PHP interprets that value as NULL. However, because the syntax dictates that any parameter following an optional one must also be optional (or have a default value), passing NULL implicitly triggers the deprecation warning.

The core issue is that you are calling the function like this:

$allItems = getAllFrom('*', 'items', 'where Approve = 1', '', 'Item_ID');

Notice how the arguments map to the parameters? The third argument ('where Approve = 1') maps to $where. The fourth argument ('') maps to $and. The fifth argument ('Item_ID') is being passed to $orderfield. While this works syntactically in older PHP versions, it breaks the contract established by default parameters.

The Solution: Reordering or Adding Defaults

To resolve this warning, you must ensure that all parameters with default values appear after all required parameters. There are two primary approaches to fixing this specific function.

Approach 1: Add a Default Value

The quickest fix is to give the $orderfield parameter a default value. Since your usage implies that an order field is almost always needed, you might choose a sensible default like NULL or a generic sorting column if one exists in every table. However, if the logic requires a specific field, this approach suggests a flaw in the function's design regarding required arguments.

// Modifying the definition to accept NULL by default
function getAllFrom($field, $table, $where = NULL, $and = NULL, $orderfield = NULL, $ordering = "DESC") {
    global $con;
    
    // Safety check: Ensure orderfield is provided if critical for sorting logic
    if ($orderfield === NULL && !is_null($ordering)) {
        throw new InvalidArgumentException("Order field is required when ordering is specified.");
    }

    $getAll = $con->prepare("SELECT $field FROM $table $where $and ORDER BY $orderfield $ordering");
    $getAll->execute();
    $all = $getAll->fetchAll();
    return $all;
}

Approach 2: Refactoring for Clarity and Safety

A more robust solution involves changing the function signature to accept an array of parameters or strictly enforcing that $orderfield comes before optional ones. Given the SQL construction logic, it is often cleaner to pass the sorting criteria as a separate argument or include it in the initial call if it varies frequently.

If you decide to keep the current signature structure but simply add a default value to satisfy the parser:

function getAllFrom($field, $table, $where = NULL, $and = NULL, $orderfield = 'Item_ID', $ordering = "DESC") {
    global $con;
    
    // Validate inputs before execution to prevent SQL errors later
    if (!$field || !$table) {
        throw new InvalidArgumentException("Field and table are required.");
    }

    $getAll = $con->prepare("SELECT $field FROM $table");
    
    // Handle optional WHERE clause
    if ($where) {
        // Ensure proper SQL injection protection by using bound parameters here if possible,
        // though constructing strings dynamically requires careful sanitization.
        // In production, prefer using placeholders: $getAll->execute([], '...');
        $getAll = $con->prepare("SELECT $field FROM $table WHERE " . $where);
    }

    // Handle AND clause similarly
    if ($and) {
        if ($where) {
            $getAll = $con->prepare("SELECT $field FROM $table WHERE " . $where . " AND " . $and);
        } else {
            $getAll = $con->prepare("SELECT $field FROM $table WHERE " . $and);
        }
    }

    if ($orderfield) {
        $getAll = $con->prepare($getAll->queryString . " ORDER BY $orderfield $ordering"); 
        // Note: Constructing SQL strings dynamically is risky. In a real-world scenario,
        // you would bind the order field value to prevent injection if it comes from user input.
    }

    $getAll = $con->prepare("SELECT $field FROM $table WHERE ({$where}) ({$and}) ORDER BY $orderfield $ordering");
    
    // Resetting logic for simplicity based on original intent but with fixed signature:
    $sql = "SELECT {$field} FROM {$table}";
    if ($where) $sql .= " WHERE {$where}";
    if ($and) $sql .= " AND {$and}";
    if ($orderfield) $sql .= " ORDER BY {$orderfield} {$ordering}";

    $getAll = $con->prepare($sql);
    $getAll->execute();
    return $getAll->fetchAll();
}

Critical Security Consideration

While fixing the deprecation warning is immediate, the underlying code pattern presents a significant security vulnerability. The function constructs SQL queries by directly concatenating user-provided variables into the query string:

$getAll = $con->prepare("SELECT $field FROM $table $where $and ORDER BY $orderfield $ordering");

If $field, $table, $where, or $orderfield originate from user input, this is a prime target for SQL Injection. The prepare() method does not protect against injection if the variables themselves are interpolated into the string before preparation.

For instance, if an attacker controls the value passed to $orderfield, they could potentially manipulate the query structure. Always use bound parameters for values that change per request, even if the column name is dynamic. If you must pass dynamic identifiers like table names or column names, validate them strictly against a whitelist of allowed values before concatenation.

Best Practices for Function Design

When designing utility functions like getAllFrom:

  1. Order Matters: Always define required parameters before optional ones.
  2. Fail Fast: Throw exceptions if required arguments are missing rather than letting the function fail silently or produce invalid queries.
  3. Security First: Never trust input for SQL identifiers. Validate column names and table names against a known list of safe strings.

By adjusting the function signature to place $orderfield before the optional parameters, or by assigning it a default value that aligns with your application's logic, you eliminate the deprecation warning and adhere to PHP's strict type and parameter handling standards.

Our network