Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:125099 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 00D7C1A00BD for ; Thu, 22 Aug 2024 21:40:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1724362959; bh=piJYIz0MGNRLmOrAOD00ROQrxdDaS2Umm6VT4CXti10=; h=Date:Subject:To:References:From:In-Reply-To:From; b=ag4/DjHRNG0nM0XHWmbPm3GSMYS04+iKBcEi8n11SuYEegZMwXVpWIgGbpU1fDNsk oJQlM0ZAVB8bjoELXndLrhRuRna8p5svKeorICH+MpCEFEhWJn6VQoVu7RDSjq4FwM GI2ntMVcLQ4cnyJymkPMd0ZNz3VOGTU7nSivkgy4EabCwPamk1dvgZAhgRejq2W27Y H59Pohi3LjFrAcdufKSpm0ZXBqLdO1bUNiryEZSJkDph+JIbsnGx+pZKkJadHDVzWI W27uuhTuYy6yTYrOvB5bzhiEF7M1FIKhVw7LcRP8AwbHzVEGYp/EqPWZWx0IADqzh3 YzW/yf7NU9bEQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C4D90180034 for ; Thu, 22 Aug 2024 21:42:38 +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.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,HTML_MESSAGE, RCVD_IN_DNSWL_LOW,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fout8-smtp.messagingengine.com (fout8-smtp.messagingengine.com [103.168.172.151]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 22 Aug 2024 21:42:38 +0000 (UTC) Received: from phl-compute-01.internal (phl-compute-01.nyi.internal [10.202.2.41]) by mailfout.nyi.internal (Postfix) with ESMTP id 541371390126 for ; Thu, 22 Aug 2024 17:40:47 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-01.internal (MEProxy); Thu, 22 Aug 2024 17:40:47 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rwec.co.uk; h=cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm1; t=1724362847; x=1724449247; bh=a3qGGxQi7/ oiMc2prK44jNhjkrhwMLeSqBhAE+iyfcs=; b=IGyvR3JfE1QLmposh2w5Q+LyFB FkOB6/Ez4WCljeH0+7pDDfL3/EkfBAYsTtJnv59Zz19qNbGOhWSYC3vFddoXvZLN QTRs3cG61CFn9r/SPFJEdsDfwGoSs84BDon7TP1am0fg2kBQ+fz7AlMUR4LMMu6o t1EAz/o8b+9MZevph+VLT/mRWU76emXUHPHz+zCDEkQF/ptqESVv9CYUpuGO7b+F CbAi2bVTiUHx2C1hvCe7/To+7uXtPuNh9FvEedLJ5jYMNDX0X4MRrUqWhnY06Kz1 R1au8sO6/OdSOSDUXVRVOgRqKY0vJymlUM+xssZZz8cb9svalFdhMvUsN90w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; t=1724362847; x=1724449247; bh=a3qGGxQi7/oiMc2prK44jNhjkrhw MLeSqBhAE+iyfcs=; b=sykXeFEpjEVOc5dT4wnbTBkiT9qrHuE/wFtcdUlB/oT/ 5JpGTcV2Q3YXFfe/8IolMz9h1MgsgN8xojvmQ//aNOfi9L4NPxqmS+xa7GBRskvl cxsGjY2JwBOv/eQVflNiw1UBvRBETo9UUoZwB2Ey4Xka57Bs9EgUnr4nnGYOK81s mWPVU+xpdmqWzBExDNwYveYAQAgE3xtA+V3Eh5dElHhx3dkJdjnGgn3iNHiPvwWL PsF4dNz/KONgPRwAxTGPqmyYx1Xt3O8U8E1Gy2oqSqSoG9PHw+y1H9nL0ZD4bVSG cag6QSBTXuQiKvC0BMpmtuUs3S3m+Nw58AftvLCLkA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddruddvuddgtdduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucenucfjughrpegtkfffgg gfuffvfhfhjgesrgdtreertddvjeenucfhrhhomhepfdftohifrghnucfvohhmmhhinhhs ucglkffoufhorfgnfdcuoehimhhsohhprdhphhhpsehrfigvtgdrtghordhukheqnecugg ftrfgrthhtvghrnhepfeekvdeukeegueekhedvgfffkeeuieeutdduuedugeffvedvfeef feejjefgtdegnecuffhomhgrihhnpehgihhthhhusgdrtghomhenucevlhhushhtvghruf hiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehimhhsohhprdhphhhpsehrfigv tgdrtghordhukhdpnhgspghrtghpthhtohepuddpmhhouggvpehsmhhtphhouhhtpdhrtg hpthhtohepihhnthgvrhhnrghlsheslhhishhtshdrphhhphdrnhgvth X-ME-Proxy: Feedback-ID: id5114917:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Thu, 22 Aug 2024 17:40:46 -0400 (EDT) Content-Type: multipart/alternative; boundary="------------nkGf0qGF0MChYW6936Vbk1rr" Message-ID: <8181b3e9-4ba4-4ce8-a2c4-e41640f62975@rwec.co.uk> Date: Thu, 22 Aug 2024 22:40:45 +0100 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PHP-DEV] [Concept] Flip relative function lookup order (global, then local) To: internals@lists.php.net References: <48c22449-6cdf-449f-917f-365506e3f2d0@scriptfusion.com> Content-Language: en-GB In-Reply-To: From: imsop.php@rwec.co.uk ("Rowan Tommins [IMSoP]") This is a multi-part message in MIME format. --------------nkGf0qGF0MChYW6936Vbk1rr Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 02/08/2024 23:00, Nick Lockheart wrote: > If class A relies on class B, but you want to swap out > class B with a stub to test class A in isolation, > is there a way to make every call to class B, > from class A, actually call a different class > during the test, without modifying class A's code? There are libraries that do exactly this, such as Mockery's "overload" and "alias"; and some that do other transparent manipulations, like https://github.com/dg/bypass-finals They work by generating code dynamically, based on the real code, and executing it before the real definition is loaded. The same approach could definitely be taken to replace every call to a global function, and would actually be more reliable than shadowing, because it could rewrite even calls with a leading "\" or "use function" statement. Obviously, shadowing a function in one namespace is currently a lot easier than setting up such a rewriter; but I don't think we should let that convenience for a few use cases outweigh the benefits in performance that a change in behaviour could bring, particularly when combined with function autoloading. -- Rowan Tommins [IMSoP] --------------nkGf0qGF0MChYW6936Vbk1rr Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 7bit
On 02/08/2024 23:00, Nick Lockheart wrote:
If class A relies on class B, but you want to swap out
class B with a stub to test class A in isolation,
is there a way to make every call to class B,
from class A, actually call a different class
during the test, without modifying class A's code?


There are libraries that do exactly this, such as Mockery's "overload" and "alias"; and some that do other transparent manipulations, like https://github.com/dg/bypass-finals

They work by generating code dynamically, based on the real code, and executing it before the real definition is loaded. The same approach could definitely be taken to replace every call to a global function, and would actually be more reliable than shadowing, because it could rewrite even calls with a leading "\" or "use function" statement.

Obviously, shadowing a function in one namespace is currently a lot easier than setting up such a rewriter; but I don't think we should let that convenience for a few use cases outweigh the benefits in performance that a change in behaviour could bring, particularly when combined with function autoloading.

-- 
Rowan Tommins
[IMSoP]
--------------nkGf0qGF0MChYW6936Vbk1rr--