Skip to content

pipe operator (|>) fails to correctly handle returning by reference #19476

Closed

Description

@azjezz

Description

There appears to be a bug in how the new pipe operator (|>) handles functions that return by reference. An expression using the pipe operator is not behaving identically to its equivalent standard function call, specifically when a reference is expected to be returned.

The expression "foo" |> get_ref(...) should, in theory, be functionally equivalent to $c = get_ref(...); $c("foo");. However, the two produce different results when the function is declared to return a reference.

This code correctly returns a reference to the static variable within get_ref without any errors ( https://3v4l.org/Ojaah ):

<?php

function &get_ref($_): string {
    static $a = "f";
    return $a;
}


function &get_ref2(): string {
    $c = get_ref(...);    
    return  $c("foo");
}

echo get_ref2();

This version, which should be equivalent, incorrectly throws a notice ( https://3v4l.org/po5A6 ):

<?php

function &get_ref($_): string {
    static $a = "f";
    return $a;
}


function &get_ref2(): string {
    return "foo" |> get_ref(...);
}

echo get_ref2();

PHP Version

master

Operating System

N/A

Activity

  1. added a commit that references this issue on Aug 14, 2025
    784755f
  2. added a commit that references this issue on Aug 14, 2025
    6009b8a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions