Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120098 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 82661 invoked from network); 21 Apr 2023 09:02:19 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 21 Apr 2023 09:02:19 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id AA1441804A9 for ; Fri, 21 Apr 2023 02:02:18 -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,NICE_REPLY_A, 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-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (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 02:02:18 -0700 (PDT) Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-3f182d745deso15024575e9.0 for ; Fri, 21 Apr 2023 02:02:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1682067737; x=1684659737; h=content-transfer-encoding:in-reply-to:content-language:references :to:subject:from:user-agent:mime-version:date:message-id:from:to:cc :subject:date:message-id:reply-to; bh=2Zre5IVoIFidW3i0H8mFB44rSZIA+6WVEupE7TcbGLc=; b=e5dOXpZ59Wm1n6O3IRfGPx1YqVqmml/OmuTVV0lUsQwtrb/Zj4ohVZoAjIU7tlSUDX 6xSVjkrTD2xFkmQbyVtEkAPYT8spJlP7PWsxCSl5jSQ9DbLc8bzDulBjlstTXd7yVj4n JC3QpybzMAas/xxdFQwb3kVw5hFIR3Myu5sOUI1qu882Vgqx697A3nqimAiS+djxs9ft +Xyl0aZnprixgREnhWjECgnQAQ3O5KtfBYY3a/dLWMQetVOKZ19sgO8N8tuZImxDyHtt Vqh+yzG3uBn5U70bkBDWDQxjJjj7C46R5E1N867wgsG0VZL7hoqd2D9qgmpHVljQ/2h9 aDpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682067737; x=1684659737; h=content-transfer-encoding:in-reply-to:content-language:references :to:subject:from:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=2Zre5IVoIFidW3i0H8mFB44rSZIA+6WVEupE7TcbGLc=; b=Q0uPR3Dubt5zrpX10KQepUoaQqe8cQJb8Yn9FIRo0DzQ0lwaD3OPNCuP4rb0cyceLL NG8UWU+Y88mh3D+oXJhf4fufHxwlKs7pAT7rt19M4bjFqk/fsvYwiNMTqf4+vVPGOsEt QZOW90Gj4+hjGWWwCdw8UgLVAB0wk4y+QJcpNytpP87hRUP8bOA2dsl1xESoD9vQzz0c GkINR//GJrAo8tdA6ctLBHePXVVOw6OHijsTC2opiFTkvbbra9Qnx8clpgCBQgNugUbJ 1AF+B62iaRzlM0TwVi1yj0XqR5n5zZ4Nkq+H+DYdDsBEl3tLQ4nVoTQcAdC65ipqv7pn 6Ieg== X-Gm-Message-State: AAQBX9cXQCwUvdXc3jWXoGONpZeiUjJIRxVrhADwFS3jNSQqB/rw+VTI yrQ/WpkzjjR/YAxZOX6daXZs7Uj6VRg= X-Google-Smtp-Source: AKy350as/mSnua9l0IbiL8gmiNcNbAyPrfSL1+cXqkDAljSnlH4dD9A/jjKpguD2zy5cRL3Z29taaw== X-Received: by 2002:a1c:e901:0:b0:3f1:7bac:d411 with SMTP id q1-20020a1ce901000000b003f17bacd411mr1263666wmc.39.1682067736577; Fri, 21 Apr 2023 02:02:16 -0700 (PDT) Received: from [192.168.0.22] (cpc83311-brig21-2-0-cust191.3-3.cable.virginm.net. [86.20.40.192]) by smtp.googlemail.com with ESMTPSA id u7-20020a5d5147000000b002cde25fba30sm3993152wrt.1.2023.04.21.02.02.15 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 21 Apr 2023 02:02:15 -0700 (PDT) Message-ID: <6581c252-75c9-889d-75d2-eff5220216c2@gmail.com> Date: Fri, 21 Apr 2023 10:02:14 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 To: internals@lists.php.net References: Content-Language: en-GB In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [PHP-DEV] Expansion of PHP Symbols? From: rowan.collins@gmail.com (Rowan Tommins) Hi Marco! First of all, please do stick around, and keep learning and being curious. Sometimes out of the box thinking does get somewhere. That said, a lot of what you've written here is actually what already happens, and the problems are elsewhere. On 21/04/2023 04:54, Deleu wrote: > 1- Use include/require > 2- Custom Autoloading (not for functions but bear with me) > 3- Use Composer/PSR-4 The first thing to note is that these aren't *options*, they're *layers* on top of each other: to load some code from a file, you use include/require; to load some code *on demand*, you write an autoload function, which uses include/require; to let *someone else* write that function, you lay your files out according to PSR-4 and use Composer, which writes the autoload function, which uses include/require. > Here I want to raise the discussion about the little I know about PHP > symbols. If a symbol `interface Foo` is discovered and registered by PHP, > it will be usable as an interface throughout its entire execution. If the > symbol is not discovered and is used, it will trigger autoloading as a > last-chance before fatal error. Yes, when PHP needs the definition of a class/interface, etc, it looks up its name in a table. If it's not there, it can pass the name to an autoload function, and then try again (after the function has, hopefully, included a file with an appropriate definition). In fact, the table is shared between classes, interfaces, traits, and enums, which is why you can't have overlapping names between them, and why they all trigger the same autoloader. This makes sense because they're often used in the same context - e.g. in "$foo instanceof Foo", "Foo" can be a class, an interface, or an enum. 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. > Can `type Number = int|number;` be a symbol? > Can `type TwoInts = 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. > Now one questionable user experience I see is defining: > - 1 Class = 1 File > [...] > > But this user experience does not come from PHP's nature, but instead it > comes from Composer/PSR-4 because PSR-4 maps namespaces to directories and > symbols to files. Precisely. PHP doesn't need to do anything to change this, it's entirely up to users. There are ways that PHP might be able to optimise for different use cases, but the power of the autoloader being a callback function is that it can do whatever you want it to. It doesn't even need to involve files at all, if you don't want it to. Regards, -- Rowan Tommins [IMSoP]