Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122412 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 28FD21B0993 for ; Sat, 17 Feb 2024 15:51:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1708185092; bh=5JWeVgkUPL4Q2XNH6fmLOSYJ/UqGF/xXyVF6W81CB4Y=; h=In-Reply-To:References:Date:From:To:Subject:From; b=ir5IBv7H8C19xWHjUn3ZxvQHjB4jVd8XSPXBWJwki5uMk37HcK5cvny6xnaaMTcnG OVb0jtRmt2q6M1Nfh2gdi0+GavSnqQj/J1Gos/orc+2a6ZSR9NKtGitPejYiIliYab 7UVRaezEdRZv2W5iEA1CxTuh3iCD1iaIxYds7d3Q6ciKg4JYZ73T3BkiNkhSvOmJ8x 5FlDzG2Fvr0aux8jjte6MjXocJ8oSB2QFxa0H49sujv8YgraFefnZ6el5NlDwcqjdl eFI0pQzS08VxiJ330L4K8bKXd7TNdpkptUcDMCyaEFPGoAxy4qon1ZooKr7XIC8/at L6K12j6ErsRyA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 6806F1816DE for ; Sat, 17 Feb 2024 15:51:29 +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_20,BODY_8BITS, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING, RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from out2-smtp.messagingengine.com (out2-smtp.messagingengine.com [66.111.4.26]) (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 ; Sat, 17 Feb 2024 07:51:25 -0800 (PST) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id 171405C0054 for ; Sat, 17 Feb 2024 10:51:23 -0500 (EST) Received: from imap50 ([10.202.2.100]) by compute1.internal (MEProxy); Sat, 17 Feb 2024 10:51:23 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc:content-transfer-encoding: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=1708185083; x=1708271483; bh=VqZHKAbTDq9u6TSCk4Ekh gFklhEv32xBeJnWmIxVBME=; b=EBN9XYB0q+nX436etkC3kLoj1YnM/m7dVmaZm Q33SbFDYDj/JWC8dbHXaAACIdP6Hhl4EYa0avrC5Zhn6lhhEHXIBu7e3QeW0Na8V WyMkrz77U4vGfknBRcULADv2W76k7U4AnXy7qAMMaqiNa1F/qS1I4ifD4i5Jjfne 04j8pGlh0r1kPRYkrOUHh0L1cBeD221UUFqH92592Vc4NZDQVAtVCHxrRt9ZW0lx AavTS+QZdoTVWJ1ze3pnblG/a9l7qRrJTvuLUdMLH9ScIKyrKg8vlRTrX5mxhBtV o/ni0PCYbUQ8gc4i0/Nl7b5bpz4yesU31EfzeGWCywVUE0r2A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding: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=1708185083; x= 1708271483; bh=VqZHKAbTDq9u6TSCk4EkhgFklhEv32xBeJnWmIxVBME=; b=D IUKBYS9WRkRT/Bo1wEBAlDhDrbOSLFQNHZqwZVODhUTYKTnCPAItKVrhj5cFMUjL joeODeP4jSBYu8J0edISxxaccElhZMjlDQPwvq5vuMOZsyCffh6Zywok5NbJeGKD v64qh3Yc/qMnXVtAyQpbqBiHm068Vu6NvMjMDK2enmgb0Dlf16Yq8xmifS5GhqUA 0HJhHq5Obio7gGCLjB0JQWPdh7k8hWRQ13UwlhRTKvf2ac/kV2nYMGGQ1qSfRPUq 4/7SHanJWFxWlhmjR4ho3zZeqPh/bBG/W+65Ne57Yw4S3kss4H45oS6ycN4gu1HE QqzGzXnL6BL69RLwgJVYA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvdeggdektdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenog fuuhhsphgvtghtffhomhgrihhnucdlgeelmdenucfjughrpefofgggkfgjfhffhffvufgt gfesthhqredtreerjeenucfhrhhomhepfdfnrghrrhihucfirghrfhhivghlugdfuceolh grrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtohhmqeenucggtffrrghtthgvrhhnpedv vdeijedvgfduieegudffgeejjeevgfehhfeiieffjeeljedvvdehieeukefhhfenucffoh hmrghinhepfehvgehlrdhorhhgpdhpvggrkhgurdgtohhmnecuvehluhhsthgvrhfuihii vgeptdenucfrrghrrghmpehmrghilhhfrhhomheplhgrrhhrhiesghgrrhhfihgvlhguth gvtghhrdgtohhm X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id B4F921700098; Sat, 17 Feb 2024 10:51:22 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.11.0-alpha0-144-ge5821d614e-fm-20240125.002-ge5821d61 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 Message-ID: In-Reply-To: References: Date: Sat, 17 Feb 2024 09:51:02 -0600 To: "php internals" Subject: Re: [PHP-DEV] RFC Proposal : Allows calling non-static public methods through the __callStatic magic method instead of throwing an error. Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable From: larry@garfieldtech.com ("Larry Garfield") On Fri, Feb 16, 2024, at 7:56 PM, =ED=95=98=EB=8A=98=EC=95=84=EB=B6=80=EC= =A7=80 wrote: > Hi. > I'd like to propose an RFC, but I don't have the authority. > Below is my suggestion. > If you think this makes sense, please write an RFC for me. > Sincerely. > > ---------------------------------------------------------- > > =3D=3D=3D=3D=3D Introduction =3D=3D=3D=3D=3D > Allows calling non-static public methods through the __callStatic magi= c=20 > method instead of throwing an error. > > =3D=3D=3D=3D=3D Proposal =3D=3D=3D=3D=3D > > From a conceptual perspective: > It goes without saying that calling a non-static method statically wil= l=20 > result in an error. > However, a confusing situation occurred when the __callStatic magic=20 > method exists. > Non-public methods can be called, but public methods cannot. > This is the opposite of the method visibility policy. > > From a practical point of view: > If you can call Non-static public methods through the __callStatic=20 > magic method, you can write code like Laravel ORM more simply and tidy. > > > User::foo()->bar(); > > > =3D=3D=3D=3D Before =3D=3D=3D=3D > > > class Foo > { > protected static ?Foo $instance =3D null; > > public static function __callStatic($method, $args) > { > $instance =3D self::$instance ?? self::$instance =3D new stati= c(); > return $instance->__call($method, $args); > } > > public function __call($method, $args) > { > if (method_exists($this, $method)) { > return $instance->$method(...$args); > } > > return $this; > } > > protected function bar() > { > echo __METHOD__ . '
'; > > return $this; > } > > protected function baz() > { > echo __METHOD__ . '
'; > > return $this; > } > } > > Foo::bar()->baz(); > (new Foo())->bar()->baz(); >
> > There is no error, but the concept of method visibility is broken. > All Non-public methods can be called at instance scope. > > =3D=3D=3D=3D After =3D=3D=3D=3D > > > class Foo > { > protected static ?Foo $instance =3D null; > > public static function __callStatic($method, $args) > { > $instance =3D self::$instance ?? self::$instance =3D new stati= c(); > > if (method_exists($instance, $method)) { > return $instance->$method(...$args); > } > > return $instance; > } > > public function bar() > { > echo __METHOD__ . '
'; > > return $this; > } > > public function baz() > { > echo __METHOD__ . '
'; > > return $this; > } > } > > Foo::bar()->baz(); > (new Foo())->bar()->baz(); >
> > This is more tidy. > Only public methods are callable at instance scope. That calling bar() works at all in this example is rather accidental. T= he whole point of a non-static method is that it has an implicit require= d $this argument to it. Without a required argument, a method cannot be= called, because it is supposed to fail. In fact, even your "before" example fails: https://3v4l.org/moH0s I don't think what you describe is even possible, nor would it be a good= idea. See also: https://peakd.com/hive-168588/@crell/cutting-through-the-static --Larry Garfield