# RFC Proposal : Allows calling non-static public methods through the __callStatic magic method instead of throwing an error.

- Thread: https://externals.io/message/122403
- Original archive: https://news-web.php.net/php.internals/122403
- Started by daddyofsky@gmail.com on 2024-02-17T01:56:00+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #122403 — daddyofsky@gmail.com — 2024-02-17T01:56:00+00:00

URL: https://externals.io/message/122403#122403

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.

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

==== Before ====

<code>
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__ . '<br />';

        return $this;
    }

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

        return $this;
    }
}

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

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

==== After ====

<code>
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__ . '<br />';

        return $this;
    }

    public function baz()
    {
        echo __METHOD__ . '<br />';

        return $this;
    }
}

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

This is more tidy.
Only public methods are callable at instance scope.

---

## #122412 — Larry Garfield — 2024-02-17T15:51:02+00:00

URL: https://externals.io/message/122403#122412 · In reply to: #122403

> 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.
>
> <code>
> User::foo()->bar();
> </code>
>
> ==== Before ====
>
> <code>
> 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__ . '<br />';
>
>         return $this;
>     }
>
>     protected function baz()
>     {
>         echo __METHOD__ . '<br />';
>
>         return $this;
>     }
> }
>
> Foo::bar()->baz();
> (new Foo())->bar()->baz();
> </code>
>
> There is no error, but the concept of method visibility is broken.
> All Non-public methods can be called at instance scope.
>
> ==== After ====
>
> <code>
> 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__ . '<br />';
>
>         return $this;
>     }
>
>     public function baz()
>     {
>         echo __METHOD__ . '<br />';
>
>         return $this;
>     }
> }
>
> Foo::bar()->baz();
> (new Foo())->bar()->baz();
> </code>
>
> This is more tidy.
> Only public methods are callable at instance scope.

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.

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

---

## #122416 — Claude Pache — 2024-02-17T21:26:44+00:00

URL: https://externals.io/message/122403#122416 · In reply to: #122412

> Le 17 févr. 2024 à 16:51, Larry Garfield <larry@garfieldtech.com> a écrit :
> 
> 
> > […]
> 
> 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.
> 
> In fact, even your "before" example fails: https://3v4l.org/moH0s

It does work after having corrected the obvious bug in the `bar()` method.

> 
> I don't think what you describe is even possible, nor would it be a good idea.

Why wouldn’t it possible? Couldn’t the engine invoke __callStatic() instead of complaining that the targeted method is not static? (I don’t have opinion on whether it is a good idea, though)

—Claude

---

## #122420 — Jorg Sowa — 2024-02-18T00:09:09+00:00

URL: https://externals.io/message/122403#122420 · In reply to: #122416

For the reference to the others: https://3v4l.org/UmPVo

Kind regards,
Jorg
