W
W
wowbate2019-11-09 13:05:07
Laravel
wowbate, 2019-11-09 13:05:07

Why does Laravel complain about the lack of constants?

New to Laravel.
There is a class in which I am supposed to have business logic:

<?php

namespace App\Services;

class FinanceService
{
    const REVENUE  = 'plus';
    const EXPENSES = 'minus';

    public function get()
    {
     // что-то делаем
    }

At the service provider
$this->app->singleton('Finance', function (){
            return new FinanceService();
        });

Facade:
<?php

namespace App\Facades;

use Illuminate\Support\Facades\Facade;

class Finance extends Facade {

    protected static function getFacadeAccessor() {
        return 'Finance';
    }

}

And config:
'Finance' => App\Facades\Finance::class,
If you do it Finance::get(), then everything works. But if you try to get a constant Finance::REVENUE, then the error is:
Undefined class constant 'REVENUE'

What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Wells, 2019-11-09
@wowbate

Finance::get()it's simple resolve(Finance::class)->get(), so your constants won't work like that - they're constants.
Use DI instead of facades, and put your constants in a separate enum FinanceOperationTypeor similar.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question