Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122416 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 422341B01D5 for ; Sat, 17 Feb 2024 21:27:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1708205226; bh=RrnwaP7HUrOU5K35jvrn5E9LjwyKPoM0SRy9VDKsETk=; h=Subject:From:In-Reply-To:Date:Cc:References:To:From; b=fQvUx7AXKlbrBpZxVG6/aMNLjB8M99/hKPne+hpgGtUHWbY01JUmrS0VKn8XFt4FG xBKdU0NDZbmSfrqZt1ZRUxAwSQsnkJbINqbHfPwidSZazQ757m9aTJUUF/Yt3Q6NGK oHY7M/C9TdBRVIODjMpunmGEcJ/Boj+WMgnt9fvQgRwhkmuq/b8ySt0BBtdThBiKA4 q/JK5TjSOhGtYrFk3bhO9nN1o2VuuIBHb6cbsyqeyq9NU9TSBH0Oqi6UjI7xR/JmUo A/ikSVoT43v777tPxlrBF4lafwEp/fXOX3InxP4SU4+3QCFES05c+tga/mBmHZVEXk CULH5hL/ueC/w== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B2C36183CBB for ; Sat, 17 Feb 2024 21:27:02 +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, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, 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-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) (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 13:27:00 -0800 (PST) Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-4125ee9a53dso2924465e9.2 for ; Sat, 17 Feb 2024 13:26:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708205217; x=1708810017; darn=lists.php.net; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=QLSTYBxzedV2idS8CgdTqsUAsdettVJvI5QM/JGUk/w=; b=my8BNiO4DQWS1wThGoPbRQHWjRXKMkI/qcZFcgk/4eJST1ZE/eKHg94BMKByTuaZQK MYUwmrDRru2DATGS32iWb8W0Z5kLZj/q5T0kx1z9jDNsGPpA+IqyMw9YKtFJ0Fmv+EiD z3skCPKnRSerssy6GCnlD+x83qnaSEwju8OQWHTwhhBIt7EkR2bjDD9CLkewTVMYFDyJ rElgrX0wjhQtyUmaPsjOBa5WHPRAsbcASyOkoLP6lImV65vYToWTl47GyXnrudbxkIr5 FEWyfOI4O2s4jC4xzFiOM3hn0bXNKk35zKccVrRgudsx+udSE+TjDXjvqoOMWQnaZNK4 yUew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708205217; x=1708810017; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QLSTYBxzedV2idS8CgdTqsUAsdettVJvI5QM/JGUk/w=; b=irlt99Th4juKOR+YWCxXNozv/QKthwLaTL92QYf1alJdpoGAME/s8WGVTvbVwhdnFp M4itkXs/Q3Emh3mP7kaX910x01BFvMXIBWCo//mquK5MI9axXaYjwXtSfts4hgxqec1s quIWlE89AhpzgCBg7MNqcMviLa5mJvitmWjLa0UNRasVZLjd3Z/on7GDgxitzxvwPcUU Y0ARs0H/g+sJIaa1sbzux9MNk1Jenp9p7w9/5fG2f7I2O3Lggk/YW2xvqTKE64StQlOv iftQm8sR6SmeXY416E62mxqg4WwBcV5U0hwo8dVfypeJf8bf4CYDZZrDlWinuBHij16F YpZQ== X-Gm-Message-State: AOJu0YyfBf0XKolPC6RHsVKveFCAl4DF3FFo9uw06E8U9FTLNowc4KoR h1B7Gct9d8dzLZ/mYijpXRHb3z0jhe2hLq2OiGSHSMRf9Gn9AvwzNIe6lZr0 X-Google-Smtp-Source: AGHT+IG1oiRKCrUlrMuL+xdcYFEKhMvCmzc90EXwnnpYxnNYgU8BP5/y0f81iM9kb0hBQE656rLepg== X-Received: by 2002:a05:600c:4691:b0:40d:8562:aa87 with SMTP id p17-20020a05600c469100b0040d8562aa87mr6200088wmo.21.1708205216740; Sat, 17 Feb 2024 13:26:56 -0800 (PST) Received: from smtpclient.apple ([178.197.199.92]) by smtp.gmail.com with ESMTPSA id by19-20020a056000099300b0033d1ff60243sm5150000wrb.114.2024.02.17.13.26.55 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 17 Feb 2024 13:26:55 -0800 (PST) Content-Type: text/plain; charset=utf-8 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3774.300.61.1.2\)) Subject: Re: [PHP-DEV] RFC Proposal : Allows calling non-static public methods through the __callStatic magic method instead of throwing an error. In-Reply-To: Date: Sat, 17 Feb 2024 22:26:44 +0100 Cc: php internals Content-Transfer-Encoding: quoted-printable Message-ID: References: To: Larry Garfield X-Mailer: Apple Mail (2.3774.300.61.1.2) From: claude.pache@gmail.com (Claude Pache) > Le 17 f=C3=A9vr. 2024 =C3=A0 16:51, Larry Garfield = a =C3=A9crit : >=20 > 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. >>=20 >> ---------------------------------------------------------- >>=20 >> =3D=3D=3D=3D=3D Introduction =3D=3D=3D=3D=3D >> Allows calling non-static public methods through the __callStatic = magic=20 >> method instead of throwing an error. >>=20 >> =3D=3D=3D=3D=3D Proposal =3D=3D=3D=3D=3D >>=20 >> =46rom a conceptual perspective: >> It goes without saying that calling a non-static method statically = will=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. >>=20 >> =46rom 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. >>=20 >> >> User::foo()->bar(); >> >>=20 >> =3D=3D=3D=3D Before =3D=3D=3D=3D >>=20 >> >> class Foo >> { >> protected static ?Foo $instance =3D null; >>=20 >> public static function __callStatic($method, $args) >> { >> $instance =3D self::$instance ?? self::$instance =3D new = static(); >> return $instance->__call($method, $args); >> } >>=20 >> public function __call($method, $args) >> { >> if (method_exists($this, $method)) { >> return $instance->$method(...$args); >> } >>=20 >> return $this; >> } >>=20 >> protected function bar() >> { >> echo __METHOD__ . '
'; >>=20 >> return $this; >> } >>=20 >> protected function baz() >> { >> echo __METHOD__ . '
'; >>=20 >> return $this; >> } >> } >>=20 >> Foo::bar()->baz(); >> (new Foo())->bar()->baz(); >>
>>=20 >> There is no error, but the concept of method visibility is broken. >> All Non-public methods can be called at instance scope. >>=20 >> =3D=3D=3D=3D After =3D=3D=3D=3D >>=20 >> >> class Foo >> { >> protected static ?Foo $instance =3D null; >>=20 >> public static function __callStatic($method, $args) >> { >> $instance =3D self::$instance ?? self::$instance =3D new = static(); >>=20 >> if (method_exists($instance, $method)) { >> return $instance->$method(...$args); >> } >>=20 >> return $instance; >> } >>=20 >> public function bar() >> { >> echo __METHOD__ . '
'; >>=20 >> return $this; >> } >>=20 >> public function baz() >> { >> echo __METHOD__ . '
'; >>=20 >> return $this; >> } >> } >>=20 >> Foo::bar()->baz(); >> (new Foo())->bar()->baz(); >>
>>=20 >> This is more tidy. >> Only public methods are callable at instance scope. >=20 > That calling bar() works at all in this example is rather accidental. = The whole point of a non-static method is that it has an implicit = required $this argument to it. Without a required argument, a method = cannot be called, because it is supposed to fail. >=20 > In fact, even your "before" example fails: https://3v4l.org/moH0s It does work after having corrected the obvious bug in the `bar()` = method. >=20 > I don't think what you describe is even possible, nor would it be a = good idea. Why wouldn=E2=80=99t it possible? Couldn=E2=80=99t the engine invoke = __callStatic() instead of complaining that the targeted method is not = static? (I don=E2=80=99t have opinion on whether it is a good idea, = though) =E2=80=94Claude