php / php/php-src

Support for nullsafe function call syntax

Open
#14,547 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Feature Status: Needs Triage
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

The following code:

<?php
class Foo {
    public function __invoke(): string {
        return 'foo invoked';
    }
}

$f = new Foo();
var_Dump($f());

results in this expected output:

string(11) "foo invoked"
$g = null;
$g();

results in this expected output

Fatal error: Uncaught Error: Value of type null is not callable
$g = null;
$g?->__invoke();

results in this expected output:

string(11) "foo invoked"

As the call to ->__invoke() can be shortened to () I would have expected to be able to use the nullsafe operator here as well and write

$g = null;
$g?();

But this results in in this output:

Parse error: syntax error, unexpected token ")"

I would have expected

string(11) "foo invoked"

Is this on purpose? I could not find any mention of invoke in the RFC. So was this overseen?

See also https://3v4l.org/n09i2

PHP Version

All currently supported PHP-Versions

Operating System

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the reported behavior using the PHP snippets in the issue, then read the linked nullsafe operator RFC and the existing implementation and tests for nullsafe calls. Done means determining and implementing the intended handling of $g?() while preserving the documented errors for ordinary calls and matching the expected behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.