Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:106559 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 56636 invoked from network); 12 Aug 2019 16:17:14 -0000 Received: from unknown (HELO wout4-smtp.messagingengine.com) (64.147.123.20) by pb1.pair.com with SMTP; 12 Aug 2019 16:17:14 -0000 Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailout.west.internal (Postfix) with ESMTP id F35E43D2 for ; Mon, 12 Aug 2019 09:45:22 -0400 (EDT) Received: from imap26 ([10.202.2.76]) by compute7.internal (MEProxy); Mon, 12 Aug 2019 09:45:23 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; bh=hJGT2W ecd16dWYTOaKsKsMFf2KzCldkwdpi4+UJJFw0=; b=AB636eOyMyHFCgX6aNoKM1 1XeXtxPEsn3J9sG+FpbkFbEx1Nw+PBM3UDnhDOrNJi8HkRg2CIdJEUD+vW1lpOhL 7bq3JfhMpZvJIsb9gqij516Nj+zb2Z498Qv+Ktz4RPtcwcZbgBt5rIT/LiXM1GsY jhGdIUR9DNRm+p18LNgZcWairEUYlBg+VvGAzD7Ehs6ULYLVxB5ux/FlDteYEK9y YiZe6KQeIZm3NalbpLK9DHXp5spQmaTZL8Ug7wUEaQi/74zeWxiXyPN52oakDU7m /Ghjw5TgEuSHaZ76inqOfOLEVWEGtORienhwPZ1+IDm0pPhgOzUkCpOLDbcrprsQ == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduvddruddvgedgieelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgesthdtredtreertdenucfhrhhomhepfdfnrghr rhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtoh hmqeenucffohhmrghinhepghhithhhuhgsrdgtohhmnecurfgrrhgrmhepmhgrihhlfhhr ohhmpehlrghrrhihsehgrghrfhhivghlughtvggthhdrtghomhenucevlhhushhtvghruf hiiigvpedt X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id 1BE1814200A1; Mon, 12 Aug 2019 09:45:22 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.1.6-809-g8e5c451-fmstable-20190809v1 Mime-Version: 1.0 Message-ID: In-Reply-To: References: Date: Mon, 12 Aug 2019 08:45:20 -0500 To: "Steffen via internals" Content-Type: text/plain Subject: Re: [PHP-DEV] [RFC] Namespace-scoped declares, again From: larry@garfieldtech.com ("Larry Garfield") On Mon, Aug 12, 2019, at 3:26 AM, Nikita Popov wrote: > On Mon, Aug 12, 2019 at 10:17 AM Nicolas Grekas < > nicolas.grekas+php@gmail.com> wrote: > > I've read discussions about the notion of a "package" and the way we > > should define its boundaries. > > What about the following? > > > > Individual files could declare their package using this style: > > > > > That would be enough to group a set of files together and make them share > > eg some private classes, some optional PHP behaviors, etc. > > > > The right side "MyVendor\MyPackage" would also be a FQCN that PHP would > > autoload as a regular class. The corresponding class would then be the > > place where ppl would declare the engine behavior they want for their > > package (strict types, etc). To enforce this, the engine could require that > > the "MyPackage" class implements some interface/extend some base abstract > > class. > > > > Of course, one could hijack a package and declare an unrelated file as > > part of it, but I don't think that's an issue: the situation is the same as > > for namespaces, where one can hijack a third party vendor namespace. In > > practice, it proved not being an issue, and the original author's intent is > > clear: "this is my namespace/package, if you mess with it, fine, but you're > > on your own". > > > > Nicolas > > > > FTR I've created a draft-implementation for a package system here: > https://github.com/php/php-src/pull/4490 > > It uses a slightly different approach in that it keeps the package name a > string (that should usually match the Composer package name) and uses a > function to register the package. > > The main annoyance is that this requires declaring the package in every > file, something I would like to avoid. An alternative I played with is to > allow specifying the package at include time, which would allow the > autoloader to specify which package a file is part. However, while this is > more ergonomic for the user, I'm afraid that this will make static analysis > & IDE scenarios problematic, because they will not be able to easily know > what the package is in cases that fall outside convention. So in the end, > an explicit per-file package declaration may be the best we can do. > > Nikita I don't think declaring the package in each file is necessarily bad. Every language I've worked in either has you declare a package explicitly or implies it off of the file system and you don't get any say in the matter. Of the two, I prefer the former. My concern with using a function call is that it introduces/increases the potential for a file to say it uses a namespace that isn't yet defined. What happens then? Fatal? Autoload trigger? The code ends up un-packaged silently? (Please not the last one.) I don't feel like "it's Composer's problem" is a good answer here. Making the package name be a class name at least makes autoloading a natural and well understood way to handle the lookup. There may be other benefits to using a class/methods to define a package, or possibly even downsides; I'm not sure there. More research needed. Same for avoiding package name collisions; "it's FIG's problem" may be an answer, but it seems like a poor one. In practice I'd expect package definitions to be the obvious example of the preloader, but there will always be cases where the preloader is not available or cannot be used so we need to consider the performance impact of registering packages in every request. Has anyone done in-depth research into how other languages handle packages, and what advantages packages would have over just our existing nested namespaces? I feel like there's ample prior art here that we should not ignore. --Larry Garfield