Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Facade class error - Non-static method should not be called statically

Tags:

php

laravel

This is the error that I get:

Non-static method Tester\Test::echoString() should not be called statically, assuming $this from incompatible context

Here is what I have added to app.php:

ServiceProvider:

"Tester\TestServiceProvider"

And Alias:

"Test" =>  "Tester\Test"

Here is my main class. named: test.library.php:

namespace Tester;

class Test 
{
    function echoString()
    {
        echo "This is a text string";
    }

    function printer($input)
    {
        $this->echoString();
        echo " $input";
    }
}

Here is my test.facade.php file:

class Test extends \Illuminate\Support\Facades\Facade
{
    protected static function getFacadeAccessor() { return 'Test'; }
}

And here is the test.serviceprovider.php class:

use Illuminate\Support\ServiceProvider;

class TestServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->bind('Test', function()
        {
            return new \Tester\Test(); // Name of your class, be sure to include the namespace if you are using one.
        });
    }
}
like image 298
Mostafa Talebi Avatar asked Sep 05 '25 17:09

Mostafa Talebi


1 Answers

I think it's because you have a class named Test and also a Facade named Test. And it's a collision on the namespace.

.. probably rename Test (the facade) to TestFacade, then set the alias as "Test" => "Tester\TestFacade"

like image 153
rtconner Avatar answered Sep 07 '25 17:09

rtconner