Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:124763 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 ED7AE1A00EF for ; Mon, 5 Aug 2024 12:27:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1722860946; bh=wMDX7ehxmEBCI916cV9lkpvtMj8m5NvZ9GBPecuiHR8=; h=Subject:From:To:Date:In-Reply-To:References:From; b=QO+SrHaMYZ63zckHAGyr6zEBJ/iidZYknoKqq8tFO6aIHIz02ZP8QAYfH7RlzlsFn O76nWrM0xer+kwXf2fgOJhIXUQCPozGTLWiJD1f5H5PER7k8LEXoRhuvRtDMTNcx0p MubxS4pBjudiuaLt/7T5DAJfnbtHJaINtbvSO5kiYBdfGyJIB9Ft0ZaeLL/2W+I2Ww VKt7cg7u8UayIWGOZmlxROFtBf/sF0uYaqVG+gLIPt3OLUZP5D9etM+l4ZTqsaehPF hNxfToWd4L519/p7NRUOhdS8Ubo2o78A2GJ64+75dk6CenfH1me8GgHBS6XfVWHm0G j6+ex/EGY3X4A== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 29FAD180078 for ; Mon, 5 Aug 2024 12:29:06 +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.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,SPF_HELO_PASS, SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from ageofdream.com (ageofdream.com [45.33.21.21]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 5 Aug 2024 12:29:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ageofdream.com; s=ageofdream; t=1722860843; bh=wMDX7ehxmEBCI916cV9lkpvtMj8m5NvZ9GBPecuiHR8=; h=Subject:From:To:Date:In-Reply-To:References:From; b=tVS4ECymp4cGgRhWVBnNaJdYvgvmYD5DjS8dYxqYyxaXgcgtQ+1XiXIlpagiDUg/F 6mrQU+qzEdwfpYScqCCRBGsdLnf4CI/t28SmmIvlsaShQ3cElYq3B+4ZsUk6cmz641 wMfncJH6IXLdH6a4TXt5rXyN3ncGQw7n7v3MJLudNzL80PRMnviIYAKn77yBYgERp7 4QfavrRNOjGtu3TNa03tyKRaO9nMqlFTXEBOpk6pVyiHGL5HVMCfLrBryGRGAL6ogn d6J8Ob9pqYFNhBr/BLFB9cxFCwSBTNPrWox5kUVgeeZ1WKySwHHNE+VhiLYL2al6AP oUxPLyU4Fl26w== Received: from [192.168.1.7] (unknown [72.255.193.122]) by ageofdream.com (Postfix) with ESMTPSA id 7CFDF2746E for ; Mon, 5 Aug 2024 08:27:23 -0400 (EDT) Message-ID: <896ef99559e4e71d8f1c3abc47415b37ae325e55.camel@ageofdream.com> Subject: Re: [PHP-DEV] [RFC] Add Directive to Make All Namespaced Function Calls Global To: internals@lists.php.net Date: Mon, 05 Aug 2024 08:27:23 -0400 In-Reply-To: References: <1a88918e-e808-d778-45e1-53797660e093@php.net> <3563cf9b-8eab-4c82-b525-a5d2f9a767bb@varteg.nz> <38920A4B-790D-48C7-B2F6-C49D3F506232@rwec.co.uk> <0824789d-0e36-4628-85c1-4b8d9b7f86af@varteg.nz> <2244a37f-8c51-448d-8a56-329ff32e6470@bastelstu.be> <0e1a21ddef3c7da17a3539b92d5f442763f4b1f8.camel@ageofdream.com> <5f1da5195e2aea7ec41e95ff4354f50b1717d3cf.camel@ageofdream.com> <599a445530357d080cb59c9443cacd5cfd5da82f.camel@ageofdream.com> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.46.4-2 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 From: lists@ageofdream.com (Nick Lockheart) > I'm not sure your proposal solves the mocking problem. If the engine > is to interpret all non-fq calls as global or local, how would a > library include your file while switching this configuration, when it > is implemented as some directive in the file? I'm not sure I understand this question. Really, all we are doing here is telling the parser to pretend that any function call without a namespace was coded with a `\` in front of it. In other words: "Parser, if you see `array_key_exists()`, pretend you saw `\array_key_exists()` and use the dedicated opcode". The engine wouldn't change at all. The only thing changing is the opcode being generated. And the opcodes aren't changing either. The parser would do the same thing as having a backslash does now. You just wouldn't need to explicitly put the backslash there. It assumes the backslash. Or, to put it another way, we are simply specifying what the default namespace is for unqualified calls. > Also, how would only singular functions be mocked when there is no > fallback to the global scope for the rest of the functions used > within the file? That would necessitate mocking all functions, even > the unmodified ones. If a developer needed to override built-in functions (in a specific file) with local ones, and still use some built-in functions, they would then need to use the fully-qualified `\function();` to call the built-in. Consider this very brief example: // MockFileFunctions.php namespace test; function fopen{ echo 'pretending to write files!'; } // TestClass.php namespace test using local functions; class TestClass{ function TestMethod(){ // array_key_exists uses built-in due to leading \ if(\array_key_exists(...)){=20 =C2=A0 // fopen uses \test\fopen because of "using local functions" fopen('file.txt');=20 } } } In the example above, we have specified "using local functions", which prevents namespace lookup of functions, and defaults to local if the function name is unqualified. The backslash before \array_key_exists() triggers the global version, even though this file specifies "using local functions". That's because "using local functions" only applies to *unqualified* function calls and the backslash qualifies it as global. "Using local functions" would only ever be used in very special use cases like unit testing. The most common use case would be for the companion declaration: "using *global* functions". When "using global functions" is declared, the backslash is omitted from all function calls where the global function is desired, and the parser pretends a backslash is there (if there isn't a namespace specified for that function call). This boosts performance by skipping the ns lookup and by using dedicated opcodes, while also keeping code clean in the vast majority of use cases where only global functions are ever used by namespaced classes.