Skip to content

Language: new MyClass()->method() — dereferencable new without outer parens missing (PHP 8.4, RFC new_without_parentheses) #6974

Closed

Description

@PurHur

Category

language · php-src-strict · PHP 8.4

Problem

PHP 8.4 allows dereferencable new expressions — method/property/static access on a freshly constructed object without wrapping the whole expression in parentheses, as long as constructor parentheses are present:

echo new Greeter()->greet();
echo new Greeter()->property;
echo new Greeter()::staticMethod();

This compiler fails to parse (Syntax error, unexpected T_OBJECT_OPERATOR on the -> after new Class()). Valid programs must keep the old (new Greeter())->greet() form.

php-src reference

Repro

<?php
declare(strict_types=1);

class Greeter
{
    public function greet(): string
    {
        return 'hello';
    }
}

echo new Greeter()->greet(), "\n";
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php bin/vm.php test/repro-maintainer/new_chain_without_parens.php 2>&1 | head -8
'

Expected (PHP 8.4): hello
Actual: parse failure on line with new Greeter()->greet()

Scope (PHP-in-PHP first)

Layer Path
Parser / php-cfg extend new expression grammar for dereferencable ctor-args form
Compiler emit same lowering as (new Class())->member
Tests test/compliance/cases/language/new_dereferencable.phpt (method, property, static, dynamic class name)

Done when

  • Repro parses and prints hello
  • new Class()->prop, new Class()::CONST, new $dyn()->m() parity on VM
  • JIT/AOT when lowering shares desugar with parenthesized form
  • ./script/ci-fast.sh --filter new_dereferencable green on VM

Related

Activity

  1. added
    enhancementNew feature or request
    area:vmVirtual machine
    implementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claim
    on Jun 6, 2026
  2. PurHur commented on Jun 6, 2026

    @PurHur
    OwnerAuthor

    claim: worker-lane-c — starting this run

  3. PurHur commented on Jun 6, 2026

    @PurHur
    OwnerAuthor

    Merged via #6996

    Fix: NewDereferenceableDesugar in lib/Ast/ rewrites PHP 8.4 dereferencable new (new Class()->m()) to parenthesized form before nikic/php-parser v4 — same pattern as pipe/in operator desugars. Compiler lowering unchanged.

    Verification:

    ./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
    php bin/vm.php test/repro-maintainer/new_chain_without_parens.php
    vendor/bin/phpunit --filter NewDereferenceable
    vendor/bin/phpunit --filter new_dereferencable
    '

    → hello; PHPUnit OK (8 unit + 2 compliance).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:compilerCompiler / CFG / JITarea:vmVirtual machineenhancementNew feature or requestimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-2:languagePhase 2 – language features

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions