rfc:complete_callstatc_magic

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
rfc:complete_callstatc_magic [2024/03/29 02:23] daddyofskyrfc:complete_callstatc_magic [2024/03/29 04:32] (current) daddyofsky
Line 145: 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.
 +
 +----------------------
  
  
rfc/complete_callstatc_magic.1711678986.txt.gz · Last modified: 2024/03/29 02:23 by daddyofsky