rfc:complete_callstatc_magic

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
rfc:complete_callstatc_magic [2024/03/29 01:57] – created daddyofskyrfc:complete_callstatc_magic [2024/03/29 04:32] (current) daddyofsky
Line 9: Line 9:
  
 ===== Introduction ===== ===== Introduction =====
-The **_ _callStatic** magic method should be invoked when a method is not callable in static context.+The **_ _callStatic** magic method should be invoked when non-static public methods are called in static context.
  
  
Line 53: Line 53:
 However, the IDE cannot find **active** method, and the **Go to Definition** feature cannot be used. However, the IDE cannot find **active** method, and the **Go to Definition** feature cannot be used.
  
-Somewhere in the internal code, there is code that matches the **active** method to the **scopeActive** method, but it's hidden, not easy to find.+Somewhere in the internal code not in _ _callStatic, there is code that matches the **active** method to the **scopeActive** method, but it's hidden, not easy to find.
  
  
Line 128: Line 128:
     public static __callStatic($method, $args)     public static __callStatic($method, $args)
     {     {
-        return (new RealRouter())->$method(...$args);+        return (new static())->$method(...$args);
     }     }
  
-    public function prefix() {}+    public function prefix(): static 
 +    { 
 +        // some code 
 +         
 +        return $this; 
 +    }
     public function group() {}     public function group() {}
     ...     ...
Line 140: Line 145:
  
 Aside from the fact that the method is not static, this code is very clear too. Aside from the fact that the method is not static, this code is very clear too.
 +
 +
 +
 +==== Case 3 ====
 +
 +Let's make a custom facade in Laravel.
 +
 +<code php>
 +<?php
 +// 1. app/Services/CustomService.php
 +namespace App\Services;
 +
 +class CustomService
 +{
 +    public function someMethod() {}
 +}
 +</code>
 +
 +<code php>
 +<?php
 +// 2. app/Facades/CustomFacade.php
 +namespace App\Facades;
 +
 +use Illuminate\Support\Facades\Facade;
 +
 +class CustomFacade extends Facade
 +{
 +    protected static function getFacadeAccessor()
 +    {
 +        return 'custom';
 +    }
 +}
 +</code>
 +
 +<code php>
 +<?php
 +// 3. app/Providers/AppServiceProvider.php
 +namespace App\Providers;
 +
 +use Illuminate\Support\ServiceProvider;
 +use App\Services\CustomService;
 +
 +class AppServiceProvider extends ServiceProvider
 +{
 +    public function register()
 +    {
 +        $this->app->bind('custom', function () {
 +            return new CustomService();
 +        });
 +    }
 +}
 +</code>
 +
 +<code php>
 +<?php
 +use App\Facades\CustomFacade;
 +
 +CustomFacade::someMethod();
 +</code>
 +
 +
 +Now what if this RFC is accepted?
 +
 +
 +<code php>
 +<?php
 +// 1. app/Services/CustomService.php
 +namespace App\Services;
 +
 +class CustomService
 +{
 +    public static __callStatic($method, $args)
 +    {
 +        return (new static())->$method(...$args);
 +    }
 +
 +    public function someMethod() {}
 +}
 +</code>
 +
 +<code php>
 +<?php
 +use App\Services\CustomService;
 +
 +CustomService::someMethod();
 +</code>
 +
 +That's it.
 +
 +----------------------
  
  
Line 146: Line 241:
 Of course, calling non-static methods in a static-like manner can be confusing, but in modern PHP, it has already become common practice.   Of course, calling non-static methods in a static-like manner can be confusing, but in modern PHP, it has already become common practice.  
  
-This change might not be favored by the people who develop the PHP language itself, but from the perspective of a user(anonymous programmer using PHP language), it allows for easier and more flexible use.+I believe this change allows for easier and more flexible use by users (anonymous programmers using the PHP language).
  
  
 ===== Proposal ===== ===== Proposal =====
 The proposal is simple. The proposal is simple.
 +
 Instead of throwing an error when a non-static public method is called statically, the **_ _callStatic** method should be invoked. Instead of throwing an error when a non-static public method is called statically, the **_ _callStatic** method should be invoked.
  
Line 186: Line 282:
 ===== Open Issues ===== ===== Open Issues =====
 None None
- 
-===== Unaffected PHP Functionality ===== 
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
Line 193: Line 287:
  
 ===== References ===== ===== References =====
-https://github.com/php/php-src/issues/13813  +https://github.com/php/php-src/issues/13813 
 https://externals.io/message/122782 https://externals.io/message/122782
  
rfc/complete_callstatc_magic.1711677426.txt.gz · Last modified: 2024/03/29 01:57 by daddyofsky