[RFC Idea] main() entry point for PHP
| From: | fennic log | Date: | Fri, 04 Jul 2025 14:49:59 +0000 |
| Subject: | [RFC Idea] main() entry point for PHP | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-127877@lists.php.net to get a copy of this message | ||
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.