Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:127878 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by lists.php.net (Postfix) with ESMTPS id 3E3A61A00BC for ; Fri, 4 Jul 2025 15:06:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1751641470; bh=A+/akOXJ8zxaxgdJS9DYJWSSwXKP+kIWCsDf+GrYiIU=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=hrMS8+/NFoUzjbf9yo7+h1yhGIltaGQlatKNr3+llhcRATO1TkOUnp8BVkVhj36Cn f+rNMDtb9cp2avKjD+qyOcg4dBO7h1TGNLum2gA32LMRDZ6zTpRny7wD18nEbAuZ3y cdTtmyq7YueA8PI6+9KRBoCAVbIPSAYu/t+CxTL7tBidn4d5IPA3yPpiffc5roPqB8 YgjceXpzlMdpy8EZupfu12ZgHvYq4kVRhm+saQd8FMxJHKRHmL7rwuGQX21U3LWQ7f po5J6aTgXPbiC4X5dRPXMp9ZVkQO1I1ELypR/YeOkTPxDTSZ7RdNzqBdpOu+jm+Fwp XmQ254Rf4tMLw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 23A73180041 for ; Fri, 4 Jul 2025 15:04:28 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_20,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS, FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from mail-lj1-f177.google.com (mail-lj1-f177.google.com [209.85.208.177]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 4 Jul 2025 15:04:27 +0000 (UTC) Received: by mail-lj1-f177.google.com with SMTP id 38308e7fff4ca-32ca160b4bcso12606551fa.3 for ; Fri, 04 Jul 2025 08:06:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1751641578; x=1752246378; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=xi5SbGd7Ar5iXkQcP2Gr3Fb0VoUF+X5zJBkE41vxA3Y=; b=b8afxWLMghE4ZB4TeBNCMfBF+Mwwp6HyEspumppjEeI987KZX1daEkOdCSJLvC5J0X KReZlREdfF/5w5TmbbuO8BhzSa680HfVNKqipBdoD7hogRGJCoTZ5fDyI6MSYG14g1Cc r6EQy5X+/QgYBHhhZa4pofIFct+BTd0v5xzSlAuJ2BDZEVmbSpeygDQ5Dkz9SKP85eQe r9mj8LHgEJFpf9uAkwAniacsE9m20UO+2A2OFoJm3b1Syf9FoklJ5wvyh23qB/uA0u/S 8n6hUNQ9eMCihCXuAiff9rtUbCb5h+gZR1G1hMI2Vxh3qsQcI4yIXuDrp9nsY6875Tmv cd0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1751641578; x=1752246378; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=xi5SbGd7Ar5iXkQcP2Gr3Fb0VoUF+X5zJBkE41vxA3Y=; b=jaMCy1PeY1qvsMrSKEpSNmJ8PqcNO0evGZ/9Hpy0nlqRkTXAf94OKz1bDo1KnccqJ+ sJ2qDzvI6xZJB3WWNkMFzHlGcq5rkuJuHAczmObTXqfQaV4xRLY54oPU01Ysmi2jSj37 Ax5w9T16rAJDbNwFGi2lh3evDt9TFUmIfFgmgP48oaPJmA6U3veBzCiEx9MpL4uLdI5f VMup40CQDRKtKEhYL2Tr/6/3ISNimatj9XU9nULDw5BKqg3XZq/QiV8Er2IuLrluZSw3 TiFksYuwBJBTsGkFFzaH1gNf3EeCLC9uOhd7wxY2F1bCLvjhUZPQ1qGsO0afS+ZR6Di1 2rsQ== X-Gm-Message-State: AOJu0Yzg5r9mK+A8GcBuvusEi/6ZYpQGjSB6lzWhPCZXA/rmn8BRJGxq 6GTkapZLcq2McB2eDMqPhSluwE58xlyFfMJf75PgxWSxxLc55vhnvCYVZ333ydU38ckzbSQdwft USuCJfHescQiIE2LHU7I3xNGW9wnkuMw9tpNxBSg= X-Gm-Gg: ASbGncuIxZpzp0RX1HjQ9QIFOGiYm16wEFrYZ0ALEcziSByNnc5OBygGL7piaztLS/j 3eRd7urqA07DIl3VVikaOyH3hXD6/YKCc1kz8Z36rnz0pL+QKmD9Qp6+DNnv6wqQM1wFdm5i2fs YMU+Wb1K+pPHCTa8zaML02l4EJjjHvtqpoYASTboSaxcBL X-Google-Smtp-Source: AGHT+IGwi4C4ff8yvOr55f7/Y6CEgy/8a6NgOFpA4gGZcmshpfglpL29Um3w5bG5MTj3/QwJ6USxj3FMykeeEYA967U= X-Received: by 2002:a05:651c:50b:b0:32a:7610:ccdb with SMTP id 38308e7fff4ca-32e67611a10mr10756931fa.40.1751641577738; Fri, 04 Jul 2025 08:06:17 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 4 Jul 2025 17:06:06 +0200 X-Gm-Features: Ac12FXyehpb-IPIdRI-lUm-SbHUaJK-P0Io6aB5-n5DUKtmW8m77E2uCNPKru-s Message-ID: Subject: Re: [PHP-DEV] [RFC Idea] main() entry point for PHP To: fennic log Cc: PHP Developers Mailing List Content-Type: multipart/alternative; boundary="00000000000003d7d206391bd6d2" From: tekiela246@gmail.com (Kamil Tekiela) --00000000000003d7d206391bd6d2 Content-Type: text/plain; charset="UTF-8" On Fri, Jul 4, 2025, 16:50 fennic log 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. --00000000000003d7d206391bd6d2 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


On Fri, Jul 4, 2025, 16:50 fenni= c log <fenniclog@gmail.com>= ; wrote:
This is b= asically an idea I have for PHP to support a main entry point.
Where re= gardless of execution, if you run a PHP file, in CLI or Web request.=C2=A0<= /div>
If the file that is executed contains a main function, that is au= tomatically executed by the engine after any procedural=C2=A0code.=C2=A0

Any procedural code in the file, and included files = are executed before main(). I made this decision because there would be cas= es where developers want to set up=C2=A0a 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 i= nt exit code.

Example
```php
echo &qu= ot;Before main";
function main(): int
{
=C2=A0 = =C2=A0 echo "Executed in main";
=C2=A0 =C2=A0 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<= br>Executed in main
PHP returns code 0

Open ques= tions.

1.=C2=A0Should we add a declare(main_en= try_point=3Dtrue); for it to be opt-in
2. Should main() ta= ke arguments?
3. Should the main() be context aware? eg `m= ain(array $argv, int $argc)` for the CLI SAPI. I'm not=C2=A0sure what i= t would be for CGI SAPI.

Why though?
You are just saving seven keystrokes.=C2=A0
=

main();

Besides, PHP already has main function so wha= t you are proposing would be confusing.=C2=A0
=
--00000000000003d7d206391bd6d2--