3

我有一个门面(在这种情况下是一个单例),我使用以下方法注册它ServiceProvider

服务提供者

use App;

class FacilityServiceProvider extends ServiceProvider 
{

    public function register()
    {  
        $this->app->singleton('Facility', function(){
            return new Facility();
        });

        // Shortcut so developers don't need to add an Alias in app/config/app.php
        $this->app->booting(function()
        {
            $loader = \Illuminate\Foundation\AliasLoader::getInstance();
            $loader->alias('Facility', 'CLG\Facility\Facades\FacilityFacade');
        });
    }
}

正面

use Illuminate\Support\Facades\Facade;

class FacilityFacade extends Facade {

    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor() { return 'Facility'; }
}

现在,我想在我的Facility类中有静态变量:

设施.php

class Facility
{
    public static $MODEL_NOT_FOUND = '-1';

    public function __construct() { ... }
}

但是当我使用时Facility::$MODEL_NOT_FOUND,我得到了Access to undeclared static property

我究竟做错了什么?

4

1 回答 1

6

That's because the Facade class only "redirects" method calls to the underlying class. So you can't access properties directly. The simplest solution is using a getter method.

class Facility
{
    public static $MODEL_NOT_FOUND = '-1';

    public function __construct() { ... }

    public function getModelNotFound(){
        return self::$MODEL_NOT_FOUND;
    }
}

The alternative would be to write your own Facade class that extends from Illuminate\Support\Facades\Facade and make use of magic methods to access properties directly

于 2014-12-07T21:36:49.900 回答