Re: Basic Type Alias

From: Date: Fri, 27 Oct 2023 07:45:18 +0000
Subject: Re: Basic Type Alias
References: 1 2  Groups: php.internals php.internals 
Request: Send a blank email to internals+get-121480@lists.php.net to get a copy of this message
> Le 26 oct. 2023 à 21:23, Larry Garfield <larry@garfieldtech.com> a écrit : > > App-wide aliases run into the autoloading problem. If the engine runs across the add() > function above, and "numeric" isn't defined, what can it do? Currently, all it can > do is trigger autoloading, which only works for class-ish constructs (class, interface, trait, > enum). Making type aliases a full class-like construct seems... weird, and over-engineered. But if > not, how do we autoload them? And if they do autoload somehow, does that mean we end up with a > bunch of files with a single type line in them? That seems > not-good. You also then have to ask how they interact with namespaces. > > So far no one seems to like my idea of "composer has a files block, PHP has a preloader, > they're small, so who the hell cares about autoloading, just greedily load them and move on > with life", even though I think that's the best answer. :-) Hi, “Autoloading” does not mean (and has never meant) “one file per class-like construct”; even if it is the most customary strategy (and the one consecrated by PSR), it is by no means the only one. Proof of concept: With the following autoloader, at the first time the compiler needs the definition of some class-like symbol under the FooBits\Acme\Type namespace, all symbols under that namespace are loaded together: ```php spl_autoload_register(function ($class) { if (str_starts_with($class, 'FooBits\\Acme\\Type\\')) { // we have conventionally put all types defined by our library under this namespace, // and there are all defined together in the following file: require 'vendor/foobits/acme/src/types.php'; } elseif (str_starts_with($class, 'FooBits\\Acme\\')) { // for other symbols we use the boring psr-4 convention require 'vendor/foobits/acme/src/' . str_replace('\\', '/', substr($class, 13)) . '.php'; } }); ``` I think that treating the type aliases as class-like *names* for the purpose of naming and autoloading (without giving my opinion whether they are class-like *constructs* for some definition of “are”) is a reasonable approach, and autoloading/preloading/whatever-loading-strategy ought to be treated as an orthogonal (non-)issue. —Claude

« previous php.internals (#121480) next »