[RFC Idea] main() entry point for PHP

php.internals

tag Knife

1 year ago
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.

Kamil Tekiela

1 year ago
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.

Iliya Miroslavov Iliev

1 year ago
On Fri, Jul 4, 2025 at 6:09 PM Kamil Tekiela <tekiela246@gmail.com> wrote:
> > > 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. >
fennic, you don't get the concept of the `main` function. Nothing will be printed before nor after. Only what is inside it as a rule nothing else is executed outside that scope. You will end up with "headers already sent" and so on with that proposition. Kamil, you forgot to count the enter button for the new line... sheesh :)
-- Iliya Miroslavov Iliev i.miroslavov@gmail.com

tag Knife

1 year ago
On Fri, 4 Jul 2025 at 22:03, Iliya Miroslavov Iliev <i.miroslavov@gmail.com> wrote:
> > > On Fri, Jul 4, 2025 at 6:09 PM Kamil Tekiela <tekiela246@gmail.com> wrote: > >> >> >> 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. >> > > > fennic, you don't get the concept of the `main` function. Nothing will be > printed before nor after. Only what is inside it as a rule nothing else is > executed outside that scope. You will end up with "headers already sent" > and so on with that proposition. > > Kamil, you forgot to count the enter button for the new line... sheesh :) > > -- > Iliya Miroslavov Iliev > i.miroslavov@gmail.com >
I made the decision to keep precedule code to execute first due to, for example, where you need to `include(./vendor/autoload.php)` If we limit execution to only the inside main() function where main() exists, everything would break, this the keeps BC with existing codebases.

Rowan Tommins [IMSoP]

1 year ago
On 4 July 2025 15:49:59 BST, 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.
Can you explain what problem or use case this is trying to solve? As others have pointed out, if you're running procedural code and then calling "main" with no arguments, it's equivalent to adding one line of code in the file, even including the return code: exit( main() ); Is there a specific scenario where you see an automatic call being a significant improvement over that line? Rowan Tommins [IMSoP]