Call-time pass-by-reference has been removed

Call-time pass-by-reference has been removed

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

Stefan Bogdanescu

Founder & Senior Architect

Call-time pass-by-reference has been removed

If you are maintaining legacy PHP codebases or debugging scripts written for versions prior to 5.4, you will eventually encounter a specific syntax error regarding function arguments: Call-time pass-by-reference has been removed. This error typically surfaces in environments running PHP 5.4 and above, where the language specification explicitly forbids declaring parameters with an ampersand inside the function signature itself.

Consider the following pattern often found in older applications:

function myFunction( &$param ) {
    // logic here
}

In these examples, the & symbol indicates that the caller intends to pass a variable by reference. While this syntax was valid in PHP 4 and early 5.x releases, it has been deprecated and subsequently removed to enforce stricter type discipline and improve code predictability. Attempting to run such code on modern versions results in a fatal error.

The removal of call-time pass-by-reference fundamentally changes how data flows through your functions. In the older paradigm, you could define a function expecting a reference without specifying the variable at the call site: myFunction($var). The interpreter would automatically treat $var as a reference because of the & in the definition. Under the new rules, the ampersand must be explicitly present in the function call itself if you intend to pass by reference: myFunction(&$var).

However, this specific syntax error is rarely a blocker for modern development workflows. The primary reason lies in how PHP handles return values and internal references during execution. Even though the explicit & in the definition is gone, variables are still passed by reference when assigned to them internally or returned from functions. This means you can achieve similar outcomes without relying on the deprecated syntax.

When migrating code, the most common scenario involves iterating over a list of hooks or callbacks, as seen in legacy PS3 socket implementations. The original logic often looked like this:

public function trigger_hooks( $command, &$client, $input ) {
    if( isset( $this->hooks[$command] ) ) {
        foreach( $this->hooks[$command] as $func ) {
            PS3socket::debug( 'Triggering Hook \'' . $func . '\' for \'' . $command . '\'' );
            $continue = call_user_func( $func, &$this, &$client, $input );
            if( $continue === FALSE ) {
                break;
            }
        }
    }
}

In this snippet, the &$client parameter suggests an intent to modify the client object directly within the function scope. The error occurs because $client is passed as a reference in the definition of trigger_hooks. To resolve this without rewriting every legacy function signature, you must ensure that the variable is explicitly referenced where it is declared and called.

The modern, compliant approach requires updating the function definition to match the intended usage. If your goal is to mutate the client object passed from outside, define the parameter with an ampersand:

public function trigger_hooks( $command, &$client, $input ) {
    // ... logic remains the same
}

Alternatively, if you simply need to read the value without modification, remove the reference entirely. This is often cleaner unless side effects are guaranteed:

function processRequest($client) {
    // Read-only access
}

If you must maintain backward compatibility with code that relies on implicit references but cannot change the calling convention immediately, you can leverage the fact that assigning a variable to itself creates a reference. Inside the function, before using the variable, assign it to itself if mutation is required:

public function trigger_hooks( $command, $client, $input ) {
    // Create an internal reference to ensure modification affects the original
    $client =& $client; 
    
    // Now proceed with logic that modifies $client
}

While this trick works, it is generally considered bad practice in modern PHP development because it obscures intent. The preferred method is to be explicit about whether a function intends to modify its arguments. If trigger_hooks relies on the state of $client changing after execution, the signature should reflect that. If it does not, passing by value is safer and prevents accidental side effects.

For projects using frameworks like Laravel, this specific syntax error is less common because modern frameworks enforce strict typing and discourage mutable parameters in controller actions or service methods unless necessary. However, when integrating legacy libraries or writing low-level system utilities, understanding the shift from implicit to explicit references is crucial for avoiding fatal errors during deployment on PHP 7, 8, or higher.

Our network