Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:127904 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 2DECC1A00BC for ; Sat, 5 Jul 2025 14:02:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1751724014; bh=zOHE2kXWTsPqCFZ5ECY4JiYizVbTMVRHgMUtSCEVuDI=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=hJgTci8esfljqb3FC+XXey9CQmXjQcLcjXNvc8fsKl8t00gHD1NjzYklVY39D9ZfA zNk6k10m03dA7gDNdSMExlxlExQEeCv4AHofCkcYlGo88IBF7whXtLxWBgKGI0jq99 lZDXYCKUbxFuDbhqWzHgkkexVVqjK/vJNf1ht1MuSQj+pR9HvMglBTFT1ghGFfcEoy 7uJ/4FO2csYTdXExlvrzOLZV/sK9MspfWDM7761dmhMVg1NZGv2HtK//oyfoug6mcX 3JIo+3tAckhGjM91tZzdPAykh2L7lXkwxupCV/pRTGPzH6kcc1jnNNZzJVqDKZy5B4 Gx9UCNdgcWSIw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 755BB180069 for ; Sat, 5 Jul 2025 14:00:13 +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=1.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, FREEMAIL_REPLY,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,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-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) (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 ; Sat, 5 Jul 2025 14:00:03 +0000 (UTC) Received: by mail-pg1-f178.google.com with SMTP id 41be03b00d2f7-b390136ed88so308264a12.2 for ; Sat, 05 Jul 2025 07:01:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1751724114; x=1752328914; 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=FasQITw/UsZ/RuMbRc2Mw6Rpu3De+sSxPjmLyFe+1YY=; b=NfMHCBSg3f3GwwQJlZiUGSE0/WAjdzbJY1G+rY+/qOnQrBW7OU3FteQ+qubc51NmNT b8X1jS7WasCSLnjGwO2X3QQGzVYBUU8tuPTIwfoQ8ulyDqUVxvbIZBXDvvpT6XXwVclD Pis/A3jYkcaCiabYsFDtadl6sEYH/w0+ATCIPHn+WkMdqIE9S4VqgG9WEaqwj+LfLErJ JhVTk8L+2o53E3ayDmA/6xawbUcbmjMmlFPAw/YLfrAyLRD+1TO2jw3MDXe2fTVnPwz0 /yyhkSkLry/WkAvI+paJGCVH5cHdEY/jves4ti93eT1KvBzPhViTswdVQkpX5SHZ3YtV fcDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1751724114; x=1752328914; 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=FasQITw/UsZ/RuMbRc2Mw6Rpu3De+sSxPjmLyFe+1YY=; b=ixuKnwh8pnKTrdd67BFvzCyVaYoLqkp7cewRlGlDi9Xy5+1R8NtCrz1gBpQeNnc+Mk pxjB8H+8AgFAFi2Km5pFjAhnFW8W3meHdNHBxpFkIo67WcpHOmP8WqkvRZHaLv+jgAnd LDTOxCS0IqJxCwdCVjON/pQKmsLXM1qaIdEnzYjhx5w1tU/QCdHc8eU9/+x6JFoqpjn2 0XFqi4ActwOZx/sVL1CCvRIpe9Y4yb51EqJO4l9r+8Vi094ei6lLeT1+ghMxYCTZP12H iWTjfqldPhlwDG5d7yZPes7V3n0aoGluHIuYjeHKl2jmXvJVbrt/5n/3mL2bD7aQ6xBV uj1w== X-Forwarded-Encrypted: i=1; AJvYcCXGgi5ljocgHTGcgXG9ToGmLI8xUmpbGlgwhn1GccsOQ5M1o3n6AuiV5geOWxbpBQvAX+hEm2SiFP0=@lists.php.net X-Gm-Message-State: AOJu0YyzemifqEoNCCxqe0dL4GodPa1Fo48fB7ibzm0QFnv1UhdLRper H3dCnTazu2G/zURRk/cJ4fHu8nKHtl9G7+tnXs9SYII8Wu10dCssAjFFejDXp80+QGnMcYFlm4O goxpqHsjTIbF3I8K3nux85533PwCKjEc= X-Gm-Gg: ASbGncsG0fR1DMmp6cLUV1WuX65vuqdPrjQtaXE7e5vGLDtpSYqlX3c9C9YSHFDLQa1 WID9h9JZrBf02Si3SxauQNp2+1+LVO1fz6gpXXcibTmGI69/fzGPT+Nv3heNW0iL4WLirJqolep c50DnW+OYUsigkAIaAal9cxb6faBwCWF01atUTWmVcg6A/K3/r6Nxrcw== X-Google-Smtp-Source: AGHT+IFDFhYpoONFOWflx4Wbx2l8PAulPLo5amah+pXxRQi9WWDVh+SN35ZzOK6pH7iQIy9vd3sDszh2/mjsDn8sztM= X-Received: by 2002:a17:90b:3fc8:b0:315:9cae:bd8 with SMTP id 98e67ed59e1d1-31aba8d3d96mr2394529a91.23.1751724113677; Sat, 05 Jul 2025 07:01:53 -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: Sat, 5 Jul 2025 15:01:16 +0100 X-Gm-Features: Ac12FXyYExjC1-lx2kpgheNROe-UwXJDtOjvzSB_4PvVJgx5nj2itoX5B0WgZFM Message-ID: Subject: Re: [PHP-DEV] [RFC Idea] main() entry point for PHP To: Iliya Miroslavov Iliev Cc: Kamil Tekiela , PHP Developers Mailing List Content-Type: multipart/alternative; boundary="0000000000008a515e06392f0db7" From: fenniclog@gmail.com (fennic log) --0000000000008a515e06392f0db7 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, 4 Jul 2025 at 22:03, Iliya Miroslavov Iliev wrote: > > > On Fri, Jul 4, 2025 at 6:09=E2=80=AFPM Kamil Tekiela wrote: > >> >> >> 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 develop= ers >>> 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 in= t >>> 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=3Dtrue); 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 b= e >> 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 i= s > 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. --0000000000008a515e06392f0db7 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

On Fri, 4 Jul 2025 = at 22:03, Iliya Miroslavov Iliev <i.miroslavov@gmail.com> wrote:


<= div class=3D"gmail_quote">
On Fri, Jul= 4, 2025 at 6:09=E2=80=AFPM Kamil Tekiela <tekiela246@gmail.com> wrote:
<= blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-l= eft:1px solid rgb(204,204,204);padding-left:1ex">

On Fr= i, 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.=C2=A0
I= f the file that is executed contains a main function, that is automatically= executed by the engine after any procedural=C2=A0code.=C2=A0
Any procedural code in the file, and included files are execute= d before main(). I made this decision because there would be cases where de= velopers want to set up=C2=A0a global scope and variables.
The ma= in entry function MUST be within the executed file, any included files whic= h contain a main function are treated as user functions.

main f= unction signature: `function main(): int;` main must return an int exit cod= e.

Example
```php
echo "Before m= ain";
function main(): int
{
=C2=A0 =C2=A0 echo= "Executed in main";
=C2=A0 =C2=A0 return 0;
= }
echo "After main";
return 1; // This is ign= ored 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.<= br>
1.=C2=A0Should we add a declare(main_entry_point= =3Dtrue); for it to be opt-in
2. Should main() take argume= nts?
3. Should the main() be context aware? eg `main(array= $argv, int $argc)` for the CLI SAPI. I'm not=C2=A0sure what it would b= e for CGI SAPI.

Why though?

<= div dir=3D"auto">You are just saving seven keystrokes.=C2=A0

main();

Besides, PHP already has main function so what you ar= e proposing would be confusing.=C2=A0


fennic,= you don't get the concept of the `main` function. Nothing will be prin= ted before nor=C2=A0after. Only what is inside it as a rule nothing=C2=A0el= se is executed outside that scope. You will end up with "headers alrea= dy sent" and so on with that proposition.

Kamil, you forgot to = count the enter button for the new line... sheesh :)
=C2=A0
= --
Iliya Miroslavov Iliev
<= br>
I made the decision to keep precedule code to execute first d= ue to, for example, where you need to `include(./vendor/autoload.php)`=C2= =A0
If we limit execution to only the inside main() function wher= e main() exists, everything would break, this the keeps BC with existing co= debases.
--0000000000008a515e06392f0db7--