外墙:不应静态调用非静态方法

时间:2018-08-03 05:20:05

标签: php laravel laravel-5

我像这样创建了一个类和Facade:

Invoice.php(Facades / Invoice.php)

<?php

namespace app\Facades\;

use Illuminate\Support\Facades\Facade;

class Invoice extends Facade
{
   protected static function getFacadeAccessor()
   {
        //NEVER CALLED
        dd("TEST");

        return 'invoice';
    }
}
?>

InvoiceFacadesServiceProviders.php

<?php

namespace App\Providers;

use App;
use Illuminate\Support\ServiceProvider;

class InvoiceFacadesServiceProvider extends ServiceProvider
{
   public function boot()
   {
      //
   }
   public function register()
   {
      App::bind('invoice',function()
      {
         return new \App\Invoice\Invoice;
      });
   }
}
?>

最后是类本身:

<?php


namespace App\Invoice;

class Invoice
{
    public function testfunc()
    {
        return "This is a test";
    }
}
?>

在控制器中,我尝试通过使用如下类来对其进行测试:

use App\Invoice\Invoice;

并调用testfunc方法:

dd(Invoice::testfunc());

app.php中,我添加了以下内容(提供者):App\Providers\InvoiceFacadesServiceProvider::class, 和(别名):'invoice' => App\Facades\Invoice::class,

但是我只能得到:

Non-static method App\Invoice\Invoice::testfunc() should not be called statically.

我还注意到,getFacadeAccessor()方法从未触发过,因为它没有命中我放在那里的dd

我在这里想念什么?

感谢您的帮助和指导!

1 个答案:

答案 0 :(得分:2)

您将类称为Invoice,而不是Facace Invoice。这就是警告发生的原因。

尝试调用\App\Facades\Invoice::testfunc();,然后通过Facade调用该方法。