Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122785 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 B27C41A009C for ; Thu, 28 Mar 2024 10:33:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1711622040; bh=UN5WKH+eQSfdsZk5pLtE7/IcDVoLqJEKhAokU/y31eA=; h=Date:To:From:Cc:Subject:In-Reply-To:References:From; b=F3KbPFLJJp9WmA5o6bLC/JuK4EshyUGijgUSE+wV9lrAqmBK5hDmY/xrbj905szIw dNjCG5YNmYlsEg1KNx07u0+InR/U6gD4t2u2NbaOQXGXY8p1ILZ4ax/0pn+5+0DB1S sGo9k6rhoX2cAHLCNn6bT8OlFehCpAMKCZLyaXAhhBtcWS22TwN2DAk1TU8uie13tu rNoBsBpL2W8s+9RsbDSrG1NtyJt6NSUJyu9cupqI7/sYmUGzIvn0DealXDCNk/kLZU LYja90I5aPJxN6GNp2YoYWojYjhreitRVNaHweu+yrFd5D16VO7h+4rzzuu3PAa/yI FkArIux7ItDZA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8DBCF180061 for ; Thu, 28 Mar 2024 10:33:58 +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=2.1 required=5.0 tests=BAYES_50,BODY_8BITS, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-4317.proton.ch (mail-4317.proton.ch [185.70.43.17]) (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, 28 Mar 2024 10:33:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chstudio.fr; s=protonmail2; t=1711622009; x=1711881209; bh=ZxrXLTm32rNUeAKq3s4OSnO/cBhH2zdAQ88Hn91mDOs=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=qEzpDiKC9EI4pnHjDU7ltbYjJYbMrYFkP54L0GUsgwdmXfgBacPe5YOD3MUpkxd6c /hGfWb1nDjnyBQb1hz0MABgldcj5NVPd4n39E29gB+YZiEYid/6iiix91Dyd5eErzG 1bP3WQeQ44HuyqINU7ZjI0qK3m94cuCx2LDLx7XYyZ4+ZwS14dRKh2C+7dRaXBC/YK 3VGQIGucj/Tnjo/Z1/gXOcjAsJJgxVe7p++4+Jc7VKjSARrj8R9DzPzTfjQmeBF5hX 6goBndRqOH30/pxjU4C9is0ecLowTrbfQX+4fEdwvb/H9tURYMfijKkZstMaIJRc0d WJeEl7Ccwy9WQ== Date: Thu, 28 Mar 2024 10:33:26 +0000 To: Claude Pache Cc: =?utf-8?B?7ZWY64qY7JWE67aA7KeA?= , internals@lists.php.net Subject: Re: [PHP-DEV] [RFC idea introduce] The issue where the __callStatic magic method is not called when statically calling a public method. Message-ID: <8M2JTiUj7hdga1-ztER0KecraYT8pfwzSZPv0x64iikdUvBCK7n1rPzhacfBpkd8qQOiid7gSMpKeG9Zc4jtEIcl8U6LT05ou4X1RhDgPlM=@chstudio.fr> In-Reply-To: <3ECDC57B-9D22-4A0C-8E02-5720C90EF3BA@gmail.com> References: <3ECDC57B-9D22-4A0C-8E02-5720C90EF3BA@gmail.com> Feedback-ID: 41282912:user:proton Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 Content-Type: multipart/signed; protocol="application/pgp-signature"; micalg=pgp-sha512; boundary="------fe17071dcb79ab7c12a8152ce12fcd7ec04c1444ce33e37592e701ae2cf74d63"; charset=utf-8 From: s.hulard@chstudio.fr (=?utf-8?Q?St=C3=A9phane_Hulard?=) This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --------fe17071dcb79ab7c12a8152ce12fcd7ec04c1444ce33e37592e701ae2cf74d63 Content-Type: multipart/mixed;boundary=---------------------1f9a3d482ea8b29c5215306880b9afbf -----------------------1f9a3d482ea8b29c5215306880b9afbf Content-Transfer-Encoding: quoted-printable Content-Type: text/plain;charset=utf-8 Le jeudi 28 mars 2024 =C3=A0 10:40, Claude Pache = a =C3=A9crit=C2=A0: > = > = > > Le 28 mars 2024 =C3=A0 03:29, =ED=95=98=EB=8A=98=EC=95=84=EB=B6=80=EC=A7= =80 a =C3=A9crit : > > = > > = > > Hi. > > = > > I would like to register an RFC on the following issue. > > https://github.com/php/php-src/issues/13813 > > = > > To summarize briefly, users expect that the `__callStatic` magic metho= d will be called in all cases when a method is not static while using the = `__callStatic`. However, in reality, if the method is public, the `__callS= tatic` magic method is not called, and an error still occurs. > > = > > I would like to hear your opinions. > > I also hope someone can help with the RFC registration. > > = > > = > > -------------------------------------------------------- > > = > > ```php > > > class MyClass > > { > > public static function __callStatic($method, $args) > > { > > echo $method . "\n"; > > } > > = > > private function privateMethod() {} > > protected function protectedMethod() {} > > public function publicMethod() {} > > } > > = > > MyClass::privateMethod(); > > MyClass::protectedMethod(); > > MyClass::publicMethod(); > > ``` > > = > > Resulted in this output: > > ``` > > privateMethod > > protectedMethod > > Fatal error: Uncaught Error: Non-static method MyClass::publicMethod()= cannot be called statically in ... > > ``` > > = > > But I expected this output instead: > > ``` > > privateMethod > > protectedMethod > > publicMethod > > ``` > > = > > = > = > = > Hi, > One of the issue is that it is not possible to determine statically that= a certain call is static or not. It is possible to determine it at runtim= e; but you must be careful to articulate clearly the rules. Although it is= absolutely possible to have logical rules, I fear that they couldn=E2=80=99= t be sufficiently simple in order to avoid confusion. > = > In the following real-world (but hopefully rare) case (it is taken from = my own codebase), I am calling a method of a grandparent class. It is *not= * a static method: > = > ```php > = > foo =3D new class(/* ... */) extends Bar { > = > =C2=A0 =C2=A0 // ... > = > =C2=A0 =C2=A0 function Header() { > =C2=A0 =C2=A0 =C2=A0 =C2=A0 $grandparent_class =3D get_parent_class(pare= nt::class); > =C2=A0 =C2=A0 =C2=A0 =C2=A0 $grandparent_class::Header(); > =C2=A0 =C2=A0 =C2=A0 =C2=A0// ... rest of code > =C2=A0 =C2=A0 } > = > =C2=A0 =C2=A0// ... > }; > ``` > = > In the following more general case, I might intend to call to a static m= ethod. However, as of today, a call to a non-static method will occur if =C2= =A0`A` has a non-static accessible method `qux()` *and* `$this` is an in= stance of `A`: > = > ```php > class Foo { > =C2=A0 =C2=A0 function baz() { > =C2=A0 =C2=A0 =C2=A0 =C2=A0 A::qux(); > =C2=A0 =C2=A0 } > } > ``` > = > (In older versions of PHP, a non-static call would occur even when `$thi= s` is not an instance of `A`. Hopefully, this is no longer the case since = PHP 8.) > = > = > =E2=80=94Claude Hello ! In Laravel the static versus non static context is handled inside the diff= erent calls. So a static call on a non static method will be forwarded to = a non static instance to be handled properly. I don't see any advantages to call `__callStatic` on any method call since= the difference with `__call` is clear. Also, as Claude said, determining = that the call is static or not is not easy. Also, building Singleton is possible using the current PHP behavior. An ex= ample : ```php $name(...$arguments); } public static function __call($name, $arguments) { // Here handle the call the $name method. echo $name; } } $user =3D User::find(1); //Will output // find ``` That's the way it's done in Laravel. Also note that the find method doesn'= t exists in the User object. That's why it works, the method is truly "mag= ic". If the method is defined as non static, it'll break with the error given. St=C3=A9phane -----------------------1f9a3d482ea8b29c5215306880b9afbf-- --------fe17071dcb79ab7c12a8152ce12fcd7ec04c1444ce33e37592e701ae2cf74d63 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: ProtonMail wnUEARYKACcFgmYFR2AJkFAFamSXyqbjFiEEAbZnit5cX2dsfkYRUAVqZJfK puMAAGO0AQDEA6Uc6VDI+8QZP4ozROv3NEEDICBOItcgf6fpBpB5hwEAvPGc wrmg+i5MnjTohzKyPTG63xPW4UhoMkVNSExnkg4= =cic0 -----END PGP SIGNATURE----- --------fe17071dcb79ab7c12a8152ce12fcd7ec04c1444ce33e37592e701ae2cf74d63--