Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123989 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 qa.php.net (Postfix) with ESMTPS id 2F2591A009C for ; Fri, 28 Jun 2024 14:13:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1719584081; bh=yaNIV9aR8Er/9xGa1UYQ+H2QcjMmGv++SKaWlOmZ4bs=; h=In-Reply-To:References:Date:From:To:Subject:From; b=bOuLNEwzHLuRfNWNNZUdlw3ps4e4qVNi89wyvxkfUGCM6Yk/RCp5KH5C5bys4FdeB Pnu1g/Fm7X3xBhlf+Pe0LqnXj80izC6qX49Zvr057yYAsM2Dm96lRfeyUTaNTyFVo7 xFtQAv8M2ABSM8vV/nCpkiKa6nynrNFLKRAcAIvKyPZy5hXH0Hjzu4Mj9E7qDR7KUy i/MECl+nCnOxRMggY1YtZORbqlmuBkBiXkvXAq4La7cT62JVGY1vUqcrM3pAfdtI7z I5+w1N69B8YVSK0Yuhd5UsplAUmUA5DTmjzpjy6uXjRUQsInXuI63WzszK3nHbxhD6 Gn7+u7LtEd5bg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 71EA0180656 for ; Fri, 28 Jun 2024 14:14:40 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,RCVD_IN_DNSWL_LOW, SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from fhigh3-smtp.messagingengine.com (fhigh3-smtp.messagingengine.com [103.168.172.154]) (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, 28 Jun 2024 14:14:39 +0000 (UTC) Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailfhigh.nyi.internal (Postfix) with ESMTP id A9ABA11400D9 for ; Fri, 28 Jun 2024 10:13:20 -0400 (EDT) Received: from imap50 ([10.202.2.100]) by compute6.internal (MEProxy); Fri, 28 Jun 2024 10:13:20 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rwec.co.uk; h=cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1719584000; x=1719670400; bh=pDwuToFFPy AJqFlryfZWpX6Om8nI6PHkmZGtppEme2w=; b=PcTE0CnyiidzfatGR5HVira+F9 Ka74k8x6fuYK8S7SqPzrdZKO0eYZTGF4THC56VPgQ7JY7WN9bgowl/QSh6rbPe3Z RNUYaU1zRao8d16hJ9fcWk3sB5foRYGEZ5k1LXW9tJ0tnyP84gErHlkGnWRgDWm0 hvI6Ui0UjVAlJnypIWj7k3oNDS/VmV2m8tBvLZum1odSqokmWTFLEvNKSfLxCAEE atJh/GWgIAitJtRQ3zMaAv66be1lVLu3CtF5jyAy1BljDaZjWCOf+o4l7yfK+ANl oY8rbD6YmbcbTQQTx+w7GuyYWJiE/IPmuBbAFJVtn3aPqec5+EHS65ZCrlOQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1719584000; x=1719670400; bh=pDwuToFFPyAJqFlryfZWpX6Om8nI 6PHkmZGtppEme2w=; b=JWYg7r7qCTgXMeZUOLWP0RhVpAPmH1u2enWCgtB/HRwj KWz/86Cb4MNlCmGHwKrQFMvsneIvcAfv/Ru3BvfUnTq1/6HOue4HfT8+g8Ij7nwJ gptmcszssv6ksUHb959u62vX3sQ7XYatlzjIq0A7HgTsKdGykBk2c7hTikVc+qqc YPEK022dvJ6uQa4n2qxW9kMbv42yxP9fCcZ7CJPmYv3vdFCqN//wbGELP9GGXssQ 3yV2Is9uD2Xqp7SC0iF9sXVEwJY9+5JplKPAVjIP6XY3a7CUfF7rWKEC+kytoTnP VShk0QF5jjHf0W/EEJJtjk/luUGteZzfQTNpoFcjrw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrtdejgddujecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhepofgfggfkjghffffhvffutgesthdtre dtreertdenucfhrhhomhepfdftohifrghnucfvohhmmhhinhhsucglkffoufhorfgnfdcu oehimhhsohhprdhphhhpsehrfigvtgdrtghordhukheqnecuggftrfgrthhtvghrnheptd duieeiffelvedtudetueffgeetveetudeufefgkedvkeffiedtgfevueevheeunecuvehl uhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepihhmshhophdrph hhphesrhifvggtrdgtohdruhhk X-ME-Proxy: Feedback-ID: id5114917:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 226C51700093; Fri, 28 Jun 2024 10:13:20 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.11.0-alpha0-538-g1508afaa2-fm-20240616.001-g1508afaa Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 Message-ID: <551cd5b0-1c00-4818-a9ca-97f6b7e8c3dc@app.fastmail.com> In-Reply-To: <1917CF7C-26D8-4DBE-B05C-5AA650AC6C9F@rwec.co.uk> References: <1917CF7C-26D8-4DBE-B05C-5AA650AC6C9F@rwec.co.uk> Date: Fri, 28 Jun 2024 15:12:58 +0100 To: internals@lists.php.net Subject: Re: [PHP-DEV] [Initial Feedback] PHP User Modules - An Adaptation of ES6 from JavaScript Content-Type: text/plain From: imsop.php@rwec.co.uk ("Rowan Tommins [IMSoP]") On Fri, 28 Jun 2024, at 09:12, Mike Schinkel wrote: > >> On Jun 28, 2024 at 2:54 AM, wrote: >> I don't see any particular relationship between namespaces and autoloading, or any reason we need to throw them away to introduce different conventions for loading files. >> > > > Sure, you can make the argument they are not related, but then you have to ask if namespaces would look the way they do if it were not for the need to map them to be able to autoload symbols. I do not think they would. > > Autoloading is by-nature one symbol per file. Namespaces were designed for mapping with "/.php" to allow autoloading. > > Having worked in languages that do not require having to think about or run userland code to handle autoloading nor have to be concerned about loading in the proper order has been such a joy when compared to the pain of working PHP. Namespaces don't require autoloading, and autoloading doesn't require one file per class. To compile a program with multiple source files, in any language, you need one of two things: a) A list of files you want to compile. Maybe auto-generated, maybe done with a recursive iteration over a directory, but ultimately the compiler needs a file path to process. b) A way for the compiler to tell, based on some symbol it wants to resolve, which file should be compiled. PHP originally provided only option (a), via the include and require keywords. Autoloading adds option (b), where you provide a function which takes a class name and does *whatever you want* to find the definition. I think it might be time to re-visit the tooling around option (a), as OpCache makes the cost of eagerly loading a list of files much lower than it was when autoloading was added. That could be as simple as include_all($directory), or as fancy as include_from_manifest_file($some_crazy_binary_file_format); either could be implemented right now in userland, because it all eventually comes down to calling include or require. >> My opinions match Larry's almost exactly: I want package-level optimisation, and package-private declarations. But I don't want to rewrite my entire codebase to start using a completely different naming system. > > > I can't see how package-privates would be of any value to you *unless* you rewrite your codebase. Simple: I have private Composer packages, right now, that group all their classes under a particular namespace prefix. I want to be able to mark some classes in that namespace as "internal". I do not want to change every place that uses the existing classes to reference "ModuleName@ClassName" instead of "NamespacePrefix\ClassName", or change every "use" to "import from". > > And rewrite your entire codebase? Why? No need to rewrite if you don't need the specific features. Just because there is a new feature doesn't mean you have to use it if there is no benefit to you using it. Code doesn't existing in isolation; if Symfony Mailer is re-published as a "module", every single application that uses it needs to change their code from referencing namespaced classes, to having "import" statements. > > As for package-level optimisation, you'll need to give examples of what you mean there as I don't want to wrongly assume. Currently, OpCache only optimises per file, because it can't guarantee how files will be used together. A simple example is function fallback: if you could declare a package as "completely loaded", OpCache could replace all references to "strlen" with "\strlen", knowing that no namespaced function with that name could be added later. > >> Not to mention that working with a combination of existing namespaced packages and "new shiny module" packages is going to be inevitable, so we can't just hand-wave that away. > > > I do not follow your train of thought here. > > What specifically are you accusing me of "hand-waving away?" I didn't intend it as a personal accusation, apologies if it came across that way. What I meant was: we can't just treat namespaces and modules as completely separate things, and assume that every code file will be using one style or the other. We have to imagine the user experience when there is a mix of the two. I can't imagine it being pleasant to have a mix of "import" and "use" statements at the top of a file, with different and even conflicting semantics. > >> >1. Adding a module/package system to PHP with modern module features >> >> I find that "modern" often just means "fashionable". Please, let's be specific. >> What is different between imports and namespaces, and why is it a good thing? > > > 1. Namespaces are a parsing construct but not an AST construct beyond scoping. This has many ramifications which have often been mentioned as limitations on this mailing list. > > 2. Namespaces cannot provide code isolation and encapsulation, unlike more "fashionable" modules/packages. ;-) > > 3. Namespaces have no runtime behavior, but more "fashionable" modules/packages often do. Perhaps I didn't word the question well. What I'm really asking, as someone who's never used JS or Go modules, is why I'd want to write "import", rather than referencing a global name in a hierarchy. That's really all I mean by "making it compatible with namespace": I want "new \Foo\Bar\Baz;" to be able to refer to a "packaged" class, probably by having a way to mark all classes under "\Foo\Bar" as belonging to a particular package. > > 4. The usage of the escape character for namespace separator makes dynamic programming tedious and error prone. Sorry, not interested. >> > 5. Because of one-to-one symbol-to-file for autoloading, Namespaces by nature result in a large number of files in a large number of directories and do not allow code organization optimized for cohesiveness. See above - this is not related to namespaces. > > 6. Modules and packages are typically small-scoped to a single directory and it is a code smell to have many different packages tightly coupled, as is the case with namespaces. Forcing modules to munge with namespaces would mean most modules would be written with those code smells for years because that will be how everyone doing a quick shift from namespace to module will write their modules. Again, this is entirely about code style, and not something the language can control. Also, the JS insistence on having a separate package for every tiny function is a common source of criticism, so personally I am very happy that PHP packages are generally larger than that. > > 7. In designing modules, if modules and namespaces were munged together then every single design decision made for modules will have to be compatible with namespaces. I cannot currently know what all constraints will emerge but I can almost guarantee that modules would be less well-designed if they have to be shoehorned to be fully compatible with namespaces. > > That said, maybe the best solution is to NOT put the stake in the ground right now and say "They must be namespace compatible" or "They must not be namespace compatible" but move forward with an open mind so that we can tease out exactly how namespaces would constrain modules and and then make the decision later for what would be in the best interest of moving PHP into the future. If and when an actual problem arises, let's discuss it. >> What specifically stops us doing all the things you've been discussing around loading, and visibility, etc, in a way that's compatible with the >> 400_000 packages available on Packagist, and billions of lines of existing code? > > You speak as if I am proposing getting rid of namespaces and making those 400_000 packages available on Packagist, and billions of lines of existing code not work in PHP. Of course not. No, I'm saying that every one of those packages could benefit if we make incremental changes. I don't want to couple it so that you can't have "package private" without also switching to some new "advanced" dialect of the language, and I don't see any reason why we need to do so. Maybe package scoped declares could allow opting in to certain checks, but I don't think "is in a package" and "has been audited for a load of extra breaking changes" should be set by the same flag. Leaving the rest of your reply here, since you accidentally sent it privately: > > What I am saying is that we should design modules from a cleaner slate than namespaces, and allow solving problems that concerns for BC have always stopped PHP from solving. > > Besides, when a language evolves and adds new features, it rarely works to shoehorn existing code AS-IS into the new constructs because doing so does not take advantage of the new capabilities. Just because you have a ton of code written for namespaces doesn't mean modules should be constrained to make it easy for you to move your namespaces to modules without a redesign. > > But as I am proposing your namespaced code would continue to work exactly as before. > > By their nature, beginners would not be as likely to use modules and would be likely to stick to existing PHP style. Intermediate to advanced programmers could instead be the target market for modules. > > There has always been a divide in PHP between those who want a really advanced language and are happy to break compatibility to get there, and those who want PHP just the way it has always been. Modules could easily require typing for all things that can be typed, for example. Modules could be the thing that finally addresses the needs of intermediate to advanced developers while keeping everyone else who wants to keep PHP as more beginner friendly language happy. -- Rowan Tommins [IMSoP]