On Tue, 22 Sept 2026 at 16:29, Tim Düsterhus <[email protected]> wrote:
>
> Hi
>
> On 2026-09-22 17:21, Seifeddine Gmati wrote:
> > Just to confirm my understanding: when the file is included from
> > another script, the closure would still be returned without being
> > executed,
> > so the following would continue to work as it does today:
> >
> > ```php
> > $callable = require 'script_file.php';
> > $callable();
> > ```
> >
> > Is that correct?
>
> Yes. The only change is that the PHP CLI SAPI will make use of the
> return value of the main script rather than ignoring it. No changes for
> any other SAPI and no changes to `include`/`require`. Please see the
> tests in the PR, and in particular
> `sapi/cli/tests/primary_script_closure_004.phpt`.
>
> Best regards
> Tim Düsterhus

Hi Tim,

That makes sense, thanks.

Another approach worth considering is the one used by HHVM/Hack, where
an entry point is identified using an attribute:

```hack
<<__EntryPoint>>
function main(): void {
    echo "Hello world!\n";
}
```

The only documentation reference I could find is here:
https://docs.hhvm.com/hack/contributing/code-samples/#basic-usage

Hack prefixes built-in and special attributes with `__`. The PHP
equivalent might look something like this:

```php
#[EntryPoint]
function main(): void {
    echo "Hello world!\n";
}
```

A function carrying this attribute in the main script would be
executed automatically. This makes the entry point explicit and avoids
assigning special meaning to the script's return value.

There would be a few semantic questions to resolve:

1. Should top-level code in the main script execute before the
entry-point function?
2. Which signatures should be accepted? Possibilities might include
`()`, `(array $argv)`, or `(int $argc, array $argv)`, with a return
type of `void`, `int`, or `never`.
3. What should happen if multiple functions are marked as entry
points? A compile-time error seems reasonable.

I still like the returned-closure proposal, but I think this
alternative is worth considering.

Best regards,
Seifeddine

Reply via email to