Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:127520 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 E83F41A00BC for ; Sun, 1 Jun 2025 05:26:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1748755473; bh=voQB/4p/2kgWbt2wNhp4GOog95WNoyZQ6YA1vx5o5vM=; h=References:In-Reply-To:From:Date:Subject:To:From; b=MHKWSuXuw8UIRSGuzTgMsythFCQvq75AFEsZj2zamUePwXDzW1NTvqI7OCJIR7SdT gLkpT0DNu5ifhavwYJig7oNowVFWpLtgU25vZXWsPGvfGdYfmFCyE/K3l7Owl6Q++O L7uFANIoY6aGJOZJ1VUt8P/KausE/HDA2OxQV5DwPTNuYkMAWwyaR37noedLRdC5M7 NU4Ve66WELw/FMUCAbtio4F3PDVEV+Tw5uoOvT3HugyMzFBd9dh9wXr+uCBcHX6vxI sFIrN+fuPSQwitZQ3OloTfEps4MscR8ErejCwtvi9uT3edqsLV3MLAjx3IY8ASV/I7 0egb5qbgSxV1w== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id ADB371801D4 for ; Sun, 1 Jun 2025 05:24:32 +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.4 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,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-qv1-f49.google.com (mail-qv1-f49.google.com [209.85.219.49]) (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 ; Sun, 1 Jun 2025 05:24:32 +0000 (UTC) Received: by mail-qv1-f49.google.com with SMTP id 6a1803df08f44-6fad3400ea3so18228126d6.0 for ; Sat, 31 May 2025 22:26:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1748755596; x=1749360396; darn=lists.php.net; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=8SDDgxRvyIfwk2diIIkuEQiHwoJRRUzv8eZc0+/XOAk=; b=iDr/ONsfI272S72VBUTtr2NDxTkgDZmT7vxzMW3g5bFNFod1MOCbQfulw/sd0VgGWZ dQ+aReWJu8hYoso+bGNbjYEdz1S4IMVJKR+2WJ2R77w5ul5aP2HiHI+fdJlRt5+h9xXG vdhckF7Q0jBPJDCfC1cfINqE8v00CRZjNaLH3t5yJi9p6ZwUfAKY0Kkq9Ei9eeUyAz46 GwytNogL7S73tlMFl+KTmdmMWdWI82KJrug2q0s36OcRZyagI1DPTfZ+VIyE2bz4F8Vr fazJzE/upwc0OxluK3V3Z7HrO5X7z9GGy/YvA2Oc7FkQHNfeS5zsCx/EotzG+sbo3H5j ugpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748755596; x=1749360396; h=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=8SDDgxRvyIfwk2diIIkuEQiHwoJRRUzv8eZc0+/XOAk=; b=auudHxM4ErkMywkLwYAZuuv5Hbgc+GLy+VJKLk8eL5LHvF6jxO1tYX5o3+IxVOdqai vnOzCoK8YjMdM7efW2gtZNYP7IaYtKwvjhGPTf3n0Y0/KHQIfF8c0hymVdBqE+1OBS0I KKpca0eq1XDJEfdyRrSRJGiYBYP9hBjYxTYAf8hhNs66Vy1ceumkrDj2MiUGClYW7bpu VxX1TooSKkp+MC735kjq+YreWpPVTdBtPZEywYuHFfmOR/SukUyKpdnhZV0U+oUyy3q9 xY2pAgNHM6frcQXjm+mbda21hBzXQKbs+WzEOGhNurGgNCI0RSsr48X/SVwkoREvO2Kz TVFw== X-Gm-Message-State: AOJu0YymxNabaUT54iVUEcpQQOGR9Um+4OvPUolrB389OjITybTMqi44 38iToaEGomRku2k5j4KTCC8O6LGk9UCR+BmtMir42OULyPEKh0KE3dkv/LAyh+NfdTWcIrqIlrE sYIaFKjl/b81Hjh4YjxxUAmVYjIV0mfpKnea3 X-Gm-Gg: ASbGncv/5hOOmqqeyWiEoMd5jt8KvX5yobQe50LeloKp46jnZrfGaoNFsfMA5EV38ry hjicmhSuSDNkebn+X9108nyPCifjM5SUWWnhWwhxSGjb5inn4hZA/cRTCeYrk6uN/81LQChEazh 4tzAdQ85MtSjwo3EUtoLqe7Z+i74PpxoAVzEOukMkY9hhB+uDBOOLox/qGZHtxUKg= X-Google-Smtp-Source: AGHT+IHOghU/ZTRd1+t4K9T0u/Tj84FEbgy8cVKZvdHqq0ci+UC6jMLOoFJWu0i7RCvj26ssPvw5/t4h95TvKliFscY= X-Received: by 2002:a05:6214:3003:b0:6fa:c99a:cdba with SMTP id 6a1803df08f44-6facebb25fdmr147198276d6.14.1748755596032; Sat, 31 May 2025 22:26:36 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: <78641D8B-AF1D-4912-920A-D75A37C32F05@rwec.co.uk> <354cb888-97c4-4f8c-a0da-359d1e63c0f9@rwec.co.uk> <10D95B6E-094B-4EAE-A18A-AF6B795CB352@rwec.co.uk> <2adbff61-5e11-4d39-ab5c-d7950a4550a6@app.fastmail.com> <79E7FA26-2F5A-470C-B1DF-12CC46A08FE5@rwec.co.uk> <1c6dcd84-9016-48e1-971f-de7749cbdce8@rwec.co.uk> <44F59416-3922-4AF4-881A-C64F2C4E9345@rwec.co.uk> <7F11844A-A98C-4843-BC94-815FBCD2B73F@garsi.de> <7840468C-F60C-4A44-AE40-16F9007EF428@rwec.co.uk> <160E9B1D-9AF6-479B-A628-A73C618D7C1B@garsi.de> In-Reply-To: <160E9B1D-9AF6-479B-A628-A73C618D7C1B@garsi.de> Date: Sun, 1 Jun 2025 01:26:24 -0400 X-Gm-Features: AX0GCFuoYOYo81uw5anjDl-_W1WRdg3WEVvEP2noNZXLTuzSS776RVNV435ogy4 Message-ID: Subject: Re: [PHP-DEV] Module or Class Visibility, Season 2 To: PHP Internals Content-Type: multipart/alternative; boundary="00000000000019c17206367be4b9" From: tendoaki@gmail.com (Michael Morris) --00000000000019c17206367be4b9 Content-Type: text/plain; charset="UTF-8" Ok, the conversation is getting sidetracked, but I think some progress is being made. I started this latest iteration last year with a thread about introducing something similar to the ES module system of JavaScript to PHP. What attracts me to this particular model is that it should already be familiar to the vast majority of PHP users. Prior to ES modules browsers had no natural module import mechanic. Prior to ES modules all symbols were attached to the window. You can see this if you serve open this index.html from a server (Note that opening the file locally will result in the js being blocked by modern browser security. ) ```html ``` The above spits 1234 into the console twice. Second example - let's put a module in. ```html ``` This outputs 1234 twice and an error is raised about b being undefined. I bring the above up to demonstrate that is the desired behavior of what I originally called a PHP module and have been bullied over and taken to task about not understanding the meaning of "module". Rowain seems to be more comfortable characterizing this as containers. If everyone is happy with that term I really don't care - I just want a way to isolate a code block so that whatever happens in there stays in there unless I explicitly export it out, and the only way I see things in that scope is if I bring them in. The other thing that was done with ES is that the syntax for the modules was tightened. JavaScripters cannot dictate what browser a user chooses, so the bad decisions of the early days of JS never really went away until ES came along which enforced their strict mode by default. PHP has no such strict mode - it has a strict types mode but that isn't the same thing. There are multiple behaviors in PHP that can't go away because of backwards compatibility problems, and one of those might indeed be how namespaces are handled. In PHP a namespace is just a compile shortcut for resolving symbol names. The namespace is prefixed to the start of every symbol within it. Unlike Java or C#, PHP has no concept of namespace visibility. At the end of the day it's a shortcut and its implementation happens entirely at compile time. Previously in the discussion Alwin Garside made a long but insightful post on namespaces and their workings that I've been thinking on and trying to digest for the last several days. What I've arrived at is the discussions about composer and autoloaders are indeed a red herring to the discussion. At the end of the day, PHP's include statements are a means to separate the php process into multiple files. In his email he explored some of the rewriting that could be done, and myself and Rowain have also explored this in the form of namespace pathing and aliasing. We've gotten away from the original focus of containing this code and how that would work. So once again this moron is going to take a stab at it. Container modules are created with require_module('file/path'). All code that executes as a result of this call is isolated to its container. That includes the results of any require or include calls made by the module file itself or any file it requires. Since the module file is cordoned off to its own container from the rest of the application whatever namespaces it uses are irrelevant to outside code. Any symbols created in the module will not be established in the script that made the require_module() call. Since it is coming into being with a new require mechanism it could be subjected to more efficient parsing rules if that is desired, but that's a massive can of worms for later discussion. One of those will be necessary - it will need to return something to the php code that called it. The simplest way to go about this is to just require that it have a return. So... $myModule = require_module('file/path'); or perhaps const myModule = require_module('file/path'); The module probably should return a static class or class instance, but it could return a closure. In JavaScript the dynamic import() statement returns a module object that is most similar to PHP's static classes, with each export being a member or method of the module object. Circling back to a question I know will be asked - what about autoloaders? To which I answer, what about them? If the module wants to use an autoloader it has to require one just as the initial php file that required it had to have done at some point. The container module is for all intents and purposes its own php process that returns some interface to allow it to talk to the process that spawned it. Will this work? I think yes. Will it be efficient? Hell no. Can it be optimized somehow? I don't know. --00000000000019c17206367be4b9 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Ok, the conversation is getting sidetracked, but I think s= ome progress is being made.

I started this latest iterat= ion last year with a thread about introducing something similar to the ES m= odule system of JavaScript to PHP. What attracts me to this particular mode= l is that it should already be familiar to the vast majority of PHP users. = Prior to ES modules browsers had no natural module import mechanic.=C2=A0 P= rior to ES modules all symbols were attached to the window. You can see thi= s if you serve open this index.html from a server (Note that opening the fi= le locally will result in the js being blocked by modern browser security. = )

```html
<!DOCTYPE html>
<h= tml>
=C2=A0 <head>
=C2=A0 =C2=A0 <script>
=C2=A0 = =C2=A0 =C2=A0 var a =3D 1234
=C2=A0 =C2=A0 </script>
=C2=A0 <= ;/head>
=C2=A0 <body>
=C2=A0 =C2=A0 <script>
=C2=A0= =C2=A0 =C2=A0 console.log(a)
=C2=A0 =C2=A0 =C2=A0 console.log(window.a)=
=C2=A0 =C2=A0 </script>
=C2=A0 </body>
</html><= /div>
```
The above spits 1234 into the console twice.=C2=A0 = Second example - let's put a module in.

```htm= l
<!DOCTYPE html>
<html>
=C2=A0 <head>=C2=A0 =C2=A0 <script>
=C2=A0 =C2=A0 =C2=A0 var a =3D 1234
=C2= =A0 =C2=A0 </script>
=C2=A0 =C2=A0 <script type=3D"module&= quot;>
=C2=A0 =C2=A0 =C2=A0 const a =3D 5678
=C2=A0 =C2=A0 =C2=A0 = var b =3D 9123
=C2=A0 =C2=A0 </script>
=C2=A0 </head>
= =C2=A0 <body>
=C2=A0 =C2=A0 <script>
=C2=A0 =C2=A0 =C2=A0= console.log(a)
=C2=A0 =C2=A0 =C2=A0 console.log(window.a)
=C2=A0 =C2= =A0 =C2=A0 console.log(b)
=C2=A0 =C2=A0 </script>
=C2=A0 </b= ody>
</html>
```
This outputs 1234 twice an= d an error is raised about b being undefined.

I br= ing the above up to demonstrate that is the desired behavior of what I orig= inally called a PHP module and have been bullied over and taken to task abo= ut not understanding the meaning of "module". Rowain seems to be = more comfortable characterizing this as containers. If everyone is happy wi= th that term I really don't care - I just want a way to isolate a code = block so that whatever happens in there stays in there unless I explicitly = export it out, and the only way I see things in that scope is if I bring th= em in.

The other thing that was done with ES is th= at the syntax for the modules was tightened. JavaScripters cannot dictate w= hat browser a user chooses, so the bad decisions of the early days of JS ne= ver really went away until ES came along which enforced their strict mode b= y default.=C2=A0 PHP has no such strict mode - it has a strict types mode b= ut that isn't the same thing.=C2=A0 There are multiple behaviors in PHP= that can't go away because of backwards compatibility problems, and on= e of those might indeed be how namespaces are handled. In PHP a namespace i= s just a compile shortcut for resolving symbol names. The namespace is pref= ixed to the start of every symbol within it. Unlike Java or C#, PHP has no = concept of namespace visibility. At the end of the day it's a shortcut = and its implementation happens entirely at compile time.

Previously in the discussion Alwin Garside made a long but insightfu= l post on namespaces and their workings that I've been thinking on and = trying to digest for the last several days. What I've arrived at is the= discussions=C2=A0about composer and autoloaders are indeed a red herring t= o the discussion. At the end of the day, PHP's include statements are a= means to separate the php process into multiple files. In his email he exp= lored some of the rewriting that could be done, and myself and Rowain have = also explored this in the form of namespace pathing and aliasing.

We've gotten away from the original focus of containing= this code and how that would work. So once again this moron is going to ta= ke a stab at it.

Container modules are created wit= h require_module('file/path'). All code that executes as a result o= f this call is isolated to its container. That includes the results of any = require or include calls made by the module file itself or any file it requ= ires.

Since the module file is cordoned off to its= own container from the rest of the application whatever namespaces it uses= are irrelevant to outside code. Any symbols created in the module will not= be established in the script that made the require_module() call. Since it= is coming into being with a new require mechanism it could be subjected to= more efficient parsing rules if that is desired,=C2=A0but that's a mas= sive=C2=A0can of worms for later discussion. One of those will be necessary= - it will need to return something to the php code that called it.=C2=A0 T= he simplest way to go about this is to just require that it have a return. = So...

$myModule =3D require_module('file/path&= #39;);

or perhaps

const m= yModule =3D require_module('file/path');

T= he module probably should return a static class or class instance, but it c= ould return a closure.=C2=A0 In JavaScript the dynamic import() statement r= eturns a module object that is most similar to PHP's=C2=A0static classe= s, with each export being a member or method of the module object.

Circling back to a question I know will be asked - what ab= out autoloaders?=C2=A0 To which I answer, what about them? If the module wa= nts to use an autoloader it has to require one just as the initial php file= that required it had to have done at some point.=C2=A0 The container modul= e is for all intents and purposes its own php process that=C2=A0returns som= e interface to allow it to talk to the process that=C2=A0spawned it.=C2=A0<= /div>

Will this work? I think yes. Will it be efficient?= Hell no. Can it be optimized somehow? I don't know.

--00000000000019c17206367be4b9--