Re: [RFC Idea] main() entry point for PHP
| From: | Kamil Tekiela | Date: | Fri, 04 Jul 2025 15:06:06 +0000 |
| Subject: | Re: [RFC Idea] main() entry point for PHP | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-127878@lists.php.net to get a copy of this message | ||
On Fri, Jul 4, 2025, 16:50 fennic log <fenniclog@gmail.com> wrote:
> This is basically an idea I have for PHP to support a main entry point.
> Where regardless of execution, if you run a PHP file, in CLI or Web
> request.
> If the file that is executed contains a main function, that is
> automatically executed by the engine after any procedural code.
>
> Any procedural code in the file, and included files are executed before
> main(). I made this decision because there would be cases where developers
> want to set up a global scope and variables.
> The main entry function MUST be within the executed file, any included
> files which contain a main function are treated as user functions.
>
> main function signature:
function main(): int; main must return
> an int
> exit code.
>
> Example
> ```php
> echo "Before main";
> function main(): int
> {
> echo "Executed in main";
> return 0;
> }
> echo "After main";
> return 1; // This is ignored as main() exists and its return value is the
> exit code.
> ```
>
> Expected output:
> Before main
> After main
> Executed in main
> PHP returns code 0
>
> *Open questions.*
>
> *1.* Should we add a declare(main_entry_point=true); for it to be opt-in
> *2.* Should main() take arguments?
> *3.* Should the main() be context aware? eg `main(array $argv, int
> $argc)` for the CLI SAPI. I'm not sure what it would be for CGI SAPI.
>
Why though?
You are just saving seven keystrokes.
main();
Besides, PHP already has main function so what you are proposing would be
confusing.