Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122403 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 AC1411ADA70 for ; Sat, 17 Feb 2024 08:09:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1708134975; bh=j4/Jb59JGsLDxMcYxBGulvOOolsvqm54c3HKvCYz6xw=; h=From:Date:Subject:To:From; b=Mi95eFheVImj+BB7XaG8FiTN7VZJXVZVyHhwf/8BBJpWgDeHsgrs/knGU4Fu4A3BK 0hdmehnpDnjoLjZJKmuWxqa1qLnpvZ2487KhHQWLWWgteOP+G71jL+rpiSYrlCzHhi 8D9PpVE0yAC+oJhY71gqTUdMfQW7BaspO4uVewfrTgyAmFY3QpvMWvH//W8EN7ta6/ qPs2C5ylx+MWkdF2vFnw42AS0ylFqz3i6bSMEfv5rLe1wNr8r2RTIbzP2gf/uXVoDh 0a2BLbqVq0JkCKOcsBDgCvvFeItKJuIXBqazVimKMPFX/T5FlQ/u7cDE+vhzQDZka1 drdQQ+Wuq1lzg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CA5D3181397 for ; Fri, 16 Feb 2024 17:56:14 -0800 (PST) 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.2 required=5.0 tests=BAYES_40,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,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-yb1-f176.google.com (mail-yb1-f176.google.com [209.85.219.176]) (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 ; Fri, 16 Feb 2024 17:56:14 -0800 (PST) Received: by mail-yb1-f176.google.com with SMTP id 3f1490d57ef6-dcbef31a9dbso1900204276.1 for ; Fri, 16 Feb 2024 17:56:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708134972; x=1708739772; darn=lists.php.net; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=QCh7D2nHQu+EILsPDSzaPHCy9TK5JWkQfr3XWiuMtIE=; b=D0Hg0MZ3h4PaqTyRu0D/6xSsP87EHK2akOpxL4j29YC7or+Zn+2/qeFwf+cBbcU6I+ l+uPTt7m8T/Vuy6ZNEecksBGcN2C45rPKp+1mrAHsCADXEGkG0wEbh25OsrOxAhKvdBf ZZp3SaMxvM6gM3xzueQj3+dRNiHMGh2twI7ezKB+mlaJ/Hx85MKZ5H7NDyNVJa4DOptE NnFikI7vgOu9ltCxsbR8djnZ6U04/n7uktdGKKUoj/5mPVX2xwbQ6Asb/wPsZ5VeqIs3 2ZQPGhD6XJxKgNfDYCakrPiHAI9yd9qLT4M6TXJ5152pWKvUmn9H7wUtB1i7R3YVp2vr Nh0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708134972; x=1708739772; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=QCh7D2nHQu+EILsPDSzaPHCy9TK5JWkQfr3XWiuMtIE=; b=H9sT9ACxWyUi/fB3RNtbuXi/Lmz4unG7JB+AY4czLINpooj85m4x42dTKmoFM+mnfy tuOMR5cO00nfCuhwyncVhDcS984VkPWJ9PrOuHEsj548eUF00CpPpMlFDsYGD95bjnwF D5BHdoVRwuuGgHlVtMpLefiybP5BoUUuInEBVGgFmYuWn4HgDG0yJPIHP/202qKQqgzN Q1H+bKKq5WgkFns/cOFcpm5bdgu1rWibP+zEuASw4VSjZygd8nJWoLQKAZERdM9CBmmj BArMHzFguQS5IUIWFXLDgwd+DfzEFD0OKZKJUn/inDpXIF5XtRledSb9w1Zo6PKRrpBa yyFQ== X-Gm-Message-State: AOJu0YymYJoYiH2nf8TiFOeeyodf/nKNB/gZCgs4/e1w6Y3I+YBjcu0y M3Spo0lbDus7hL8tDAl1EHG/Z6wlwh7l4SlJNh6SizOIx+H3g+jpbcpVHWH6j+nh1FeDB6GcBK/ 50AH6/ary1rs23ZxIs9bpd/8wbSkoXxv8uJyMpg== X-Google-Smtp-Source: AGHT+IEnbcql19h5R4FPcTUIPXxbG36o0TVgAJLj9k/PC6mEQfXUunMrArgqR1o7UbhwOi0FkTFNTfgcBQ+3A7huIjw= X-Received: by 2002:a25:c5cd:0:b0:dcd:905:3d17 with SMTP id v196-20020a25c5cd000000b00dcd09053d17mr6442356ybe.32.1708134971740; Fri, 16 Feb 2024 17:56:11 -0800 (PST) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 Date: Sat, 17 Feb 2024 09:56:00 +0800 Message-ID: Subject: [PHP-DEV] RFC Proposal : Allows calling non-static public methods through the __callStatic magic method instead of throwing an error. To: internals@lists.php.net Content-Type: multipart/alternative; boundary="00000000000038175006118a2a42" From: daddyofsky@gmail.com (=?UTF-8?B?7ZWY64qY7JWE67aA7KeA?=) --00000000000038175006118a2a42 Content-Type: text/plain; charset="UTF-8" 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. ---------------------------------------------------------- ===== Introduction ===== Allows calling non-static public methods through the __callStatic magic method instead of throwing an error. ===== Proposal ===== From a conceptual perspective: It goes without saying that calling a non-static method statically will result in an error. However, a confusing situation occurred when the __callStatic magic 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 magic method, you can write code like Laravel ORM more simply and tidy. User::foo()->bar(); ==== Before ==== class Foo { protected static ?Foo $instance = null; public static function __callStatic($method, $args) { $instance = self::$instance ?? self::$instance = new static(); 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. ==== After ==== class Foo { protected static ?Foo $instance = null; public static function __callStatic($method, $args) { $instance = self::$instance ?? self::$instance = new static(); 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. --00000000000038175006118a2a42 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
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 met= hods through the __callStatic magic method instead of throwing an error.
=3D=3D=3D=3D=3D Proposal =3D=3D=3D=3D=3D

From a conceptual pers= pective:
It goes without saying that calling a non-static method statica= lly will result in an error.
However, a confusing situation occurred whe= n the __callStatic magic method exists.
Non-public methods can be called= , but public methods cannot.
This is the opposite of the method visibili= ty policy.

From a practical point of view:
If you can call Non-st= atic public methods through the __callStatic magic method, you can write co= de like Laravel ORM more simply and tidy.

<code>
User::foo= ()->bar();
</code>

=3D=3D=3D=3D Before =3D=3D=3D=3D
<= br><code>
class Foo
{
=C2=A0 =C2=A0 protected static ?Foo $i= nstance =3D null;

=C2=A0 =C2=A0 public static function __callStatic(= $method, $args)
=C2=A0 =C2=A0 {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 $instance= =3D self::$instance ?? self::$instance =3D new static();
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 return $instance->__call($method, $args);
=C2=A0 =C2=A0= }

=C2=A0 =C2=A0 public function __call($method, $args)
=C2=A0 = =C2=A0 {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (method_exists($this, $method)) = {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return $instance->$method= (...$args);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 }

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 return $this;
=C2=A0 =C2=A0 }

=C2=A0 =C2=A0 protected func= tion bar()
=C2=A0 =C2=A0 {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 echo __METHOD_= _ . '<br />';

=C2=A0 =C2=A0 =C2=A0 =C2=A0 return $this= ;
=C2=A0 =C2=A0 }

=C2=A0 =C2=A0 protected function baz()
=C2= =A0 =C2=A0 {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 echo __METHOD__ . '<br /= >';

=C2=A0 =C2=A0 =C2=A0 =C2=A0 return $this;
=C2=A0 =C2= =A0 }
}

Foo::bar()->baz();
(new Foo())->bar()->baz();=
</code>

There is no error, but the concept of method visib= ility is broken.
All Non-public methods can be called at instance scope.=

=3D=3D=3D=3D After =3D=3D=3D=3D

<code>
class Foo{
=C2=A0 =C2=A0 protected static ?Foo $instance =3D null;

=C2=A0= =C2=A0 public static function __callStatic($method, $args)
=C2=A0 =C2= =A0 {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 $instance =3D self::$instance ?? self:= :$instance =3D new static();

=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (method_= exists($instance, $method)) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = return $instance->$method(...$args);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 }
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return $instance;
=C2=A0 =C2=A0 }
=C2=A0 =C2=A0 public function bar()
=C2=A0 =C2=A0 {
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 echo __METHOD__ . '<br />';

=C2=A0 =C2= =A0 =C2=A0 =C2=A0 return $this;
=C2=A0 =C2=A0 }

=C2=A0 =C2=A0 pub= lic function baz()
=C2=A0 =C2=A0 {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 echo _= _METHOD__ . '<br />';

=C2=A0 =C2=A0 =C2=A0 =C2=A0 retu= rn $this;
=C2=A0 =C2=A0 }
}

Foo::bar()->baz();
(new Foo(= ))->bar()->baz();
</code>

This is more tidy.
Only = public methods are callable at instance scope.
--00000000000038175006118a2a42--