Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120101 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 93627 invoked from network); 21 Apr 2023 11:12:11 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 21 Apr 2023 11:12:11 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 652DB180503 for ; Fri, 21 Apr 2023 04:12:10 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-vk1-f173.google.com (mail-vk1-f173.google.com [209.85.221.173]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 21 Apr 2023 04:12:09 -0700 (PDT) Received: by mail-vk1-f173.google.com with SMTP id 71dfb90a1353d-43fa94bc634so14200e0c.0 for ; Fri, 21 Apr 2023 04:12:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1682075529; x=1684667529; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=O/Er2Fw7WszV1klOdQ4A9LyAiTMBH8vRXFBFqlI2A/U=; b=UG6UkK7Xpv3bNNbI+Xt2SAby20YpfIiDZ2PDjBhWksdh5tkvzB8B7dNuGJFMDw5Swt 4eUc+iDdFzcgOCp0ZBvl3V2+E9Ss4iLBMBJs1e7JufiNBnYwxtz0fmn4dmJpaWRGwj0W tw6kgVvzjM4gYmnLqMTdyB0WHoYCMPPZI6vsp0VHtIH1/qMRiSIxfqhmYo6akqs1sMnc sZYDFqu29RRoToeSfSF3X991h6rf1l6u8F7vbX2aCPXNcqUF3fd0UHeZ2YPkIhLSr8zd PX4JbJxzJenctl3KNbV8R9uhSbDbJG8Mvi6y8UPt40QrRQk1vsXCHbAfjPGZFdOji7eR tCOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682075529; x=1684667529; 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=O/Er2Fw7WszV1klOdQ4A9LyAiTMBH8vRXFBFqlI2A/U=; b=BXLKkYA4+aYDZybRxdEFJ9k+Q+S8oWRvsD9T1Jo2UCTj4Yw8Su+jCtcF/O5Q3604Yy niLmD/9laFClJMWgJN9Wg/ntu4DCxEStyU3THnXGP9pKN6OtxS/JEoRgEaUBo0YmEZ5c c6jjCloQw2Gt9GPdYASWDDI+bGMRyubYwvhzo48GChxLoLBBcXAoBacfcZRsOttoczGq IU3Aku1gD3h5AIcfvrWsMvbV+ty6JweoObnCHMw3BEbVyxuuy2hWP477m+wvsz/uejVh gE7vlebhoTtuyKy9l29BPNSNOX8W53s3oz61blZ/8LaavE7KTGhNfGIJTIsva5dnAWCM dvcg== X-Gm-Message-State: AAQBX9egZk66XEkFwv+RdtBGPl4OK7abkgwrnC4L/7oF/vSeVQjddXdx SBqZete/CnBYTtdgjNusn0plpdoXxnfjECpUzzdA9MNsjdK1aw== X-Google-Smtp-Source: AKy350aE4WsmGAUjuGvc7imW5yJV2OjG1BuRrD8YdctVBx39UE+RxXUbBeNaa2BWj6vm7VCuwIAaaZmCBCMBXKBmzf0= X-Received: by 2002:a1f:9d4d:0:b0:43f:d6a0:6542 with SMTP id g74-20020a1f9d4d000000b0043fd6a06542mr1595114vke.1.1682075528942; Fri, 21 Apr 2023 04:12:08 -0700 (PDT) MIME-Version: 1.0 References: <6581c252-75c9-889d-75d2-eff5220216c2@gmail.com> In-Reply-To: <6581c252-75c9-889d-75d2-eff5220216c2@gmail.com> Date: Fri, 21 Apr 2023 08:11:33 -0300 Message-ID: To: Rowan Tommins Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="0000000000006357d105f9d6ba92" Subject: Re: [PHP-DEV] Expansion of PHP Symbols? From: deleugyn@gmail.com (Deleu) --0000000000006357d105f9d6ba92 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, Apr 21, 2023 at 6:02=E2=80=AFAM Rowan Tommins wrote: > Hi Marco! > > I'll come back to type aliases in a second, but first: > > > can `function bar() {}` be a symbol? > > It already is. When you run foo('bar'), PHP has to look up what the > definition of "foo" is in a table of functions. In principle, > autoloading could work for those just the same way it does for classes > ... except for one awkward feature which seemed like a good idea 15 > years ago. If you write this: > > namespace Foo { > echo strlen('hello'); > } > > Should PHP lookup the function "\Foo\strlen", or the function "\strlen"? > The "clever" answer 15 years ago was to do *both*: try the namespaced > name first, then if it doesn't exist, try the global name. This does not > happen for classes, only for functions and constants. > > This is a real pain for autoloading: how do you avoid repeatedly calling > the autoloader looking for "\Foo\strlen", finding there's still no > definition, and falling back to "\strlen"? That's one of the main points > of discussion whenever function autoloading comes up. > > This is super interesting, thank you for the info. I guess maybe I'm a bit naive on why there would be controversy here. I see it kind of like this: - PHP looks up \Foo\strlen. It's not on the Symbols table. Trigger autoload for it. - Autoload finds a definition, register it on the Symbols table and execute \Foo\strlen I'm guessing this already works today. Now let's see the other case: - PHP looks up \Foo\strlen. It's not on the Symbols table. Trigger autoload for it. - Autoload does NOT find a definition, register in the Symbols table that \Foo\strlen is undefined. - PHP looks up \strlen. It's not on the Symbols table, trigger autoload for it. - Autoload finds a definition, registers in the Symbols table and executes \strlen. Since \Foo\strlen already had an autoload execution, it already had a chance to be loaded, if it wasn't we can "cache" that result. However, if the script is doing something crazy, we can let the definition of `\Foo\strlen` be overwritten if it ever gets "naturally discovered as a symbol". That is, if you e.g. try to use a class \Foo\Bar and autoloading loads the file ./foo/Bar.php and inside that file there is a definition for `\Foo\strlen`, even though `\Foo\strlen` has already been written as "undefined", the Symbols table could overwrite the "undefined" value instead of crashing with "\Foo\strlen already exists". This wouldn't go against PHP's core nature and it would avoid triggering an autoload for the same function over and over again even though it's naturally expected that it will not exist. I can imagine one would argue that caching every native function with a huge amount of namespace with a "undefined" definition could take up some memory space, but wouldn't that just encourage the PHP Community, PHP IDEs and static analysers to promote top-level file `use function \strlen`, avoid the recursive trial-and-error and avoid registering many namespaces and consuming more memory? It's already natural (and not just in PHP) for IDEs to just hide the entire import statement block, so :shrug: Here at least I can understand why it's a bit controversial. The obvious solution to me might not be an acceptable take for other people and it's somewhat understandable. > > Can `type Number =3D int|number;` be a symbol? > > Can `type TwoInts =3D callable(int $x, int $y): int;` be a symbol? > > Fundamentally, yes. They'd probably need to occupy the same lookup table > as classes - if I write "function foo(SomeType $someArg)", PHP doesn't > know until it's looked it up whether "SomeType" is a type alias or class > - but that's not a big problem. > > I think there is some discussion around whether people *want* it to work > that way - should it be more like a "use" statement, and just take > effect for the current file? Then there's questions around how to > actually implement it, and do so efficiently. But creating a table of > names to types isn't the hard part of the problem. > From Rob's email (https://externals.io/message/120094#120097), the argument against a simple "use" statement seems quite natural. I certainly don't want to redefine "use int|float as Number" in every PHP file I work with, so naturally we would go back to type alias definition, symbol registration and autoloading. So I guess my final question is: what is fundamentally different about Type Alias when compared to interfaces, classes, enums that make this controversial? --=20 Marco Deleu --0000000000006357d105f9d6ba92--