6

I have a class called Awesome and have used the ServiceProvider and the Facade to register it to the app. Now I can use it as Awesome::Things().

I want to add constants to this class, so I tried

<?php namespace Helper\Awesome;
class Awesome()
{
    public static $MOVIE = 'I love the Lego Movie!";
}

but when I call Awesome::$MOVIE, I get Access to undeclared static property: Helper\\Aesome\\Facades\\AwesomeFacade::$MOVIE

Can someone help?

Kousha
  • 32,871
  • 51
  • 172
  • 296

1 Answers1

11

The short version is -- you don't really want to do that. Laravel facades aren't mean to be used like normal classes, and if your application uses them that way you'll likely confuse future developers.

Warning out of the way. When you create a "facade" in Laravel, you're actually creating a class alias. When you added Awesome to the alias list in app/config/app.php, at some point code like the following ran

class_alias('Helper\Aesome\Facades\AwesomeFacade','Awesome');

That means whenever you use a global non-namespaced class Awesome, PHP substitutes Helper\Aesome\Facades\AwesomeFacade. If you wanted to add constants, you'd need to add them to this class.

Laravel's able to pass through methods because of the base Facade class implements a __callStatic method that passes on your call to the actual service implementation object. Facades don't pass on static constant access. Additionally, PHP does not (appear to?) have similar magic methods for passing along requests for constants.

If you're curious about the in depth version of this answer, I'm currently writing a series on Laravel's object system, including some in-depth information about the facade implementation.

Alana Storm
  • 164,128
  • 91
  • 395
  • 599
  • Okay thanks for the answer. Isn't a class model such as `User` itself a Facade? Or am I confusing that? What I want to do is create a helper function for one of my models, such as `User` (let's call it `UserHelper`. Then do all my app logic there. So for instance, I would have `UserHelper::register(...)`. That's how I am using the facades so far. – Kousha Oct 30 '14 at 20:34
  • @Kousha I'd read over the article series I listed and come back if you have question. All a facade is meant to do is provide a shortcut from something like `app()->make('someserver')->someMethod()` to something like `SomeService::someMethod`. The second syntax is called a facade in laravel. An eloquent model like `User` is **not** a facade, although Eloquent models also use static syntax. – Alana Storm Oct 30 '14 at 20:41
  • I am reading your article at the moment! Thanks a lot. Just one quick thing: If I were to make a helper function to help out with the controller logic, what do you call that? How can I use the elegant way of static syntax with it? – Kousha Oct 30 '14 at 20:43
  • @Kousha Laravel doesn't have an opinion on how you'd do that. If it were me I'd just define a \Namespaced\Helper\Class somewhere, define static methods on it (if that's the syntax you wanted to use), and then at the top of the controller file do a `use Namespaced\Helper\Class`. This would let you access the static class methods in your controller file with `Class::foo` – Alana Storm Oct 30 '14 at 21:08
  • 1
    I read your article. I'm still confused as to why I can't use Facades. I want to create the static method for my classes, and Facade seems like the best, and simplest way of doing it. If I can't use this, then what can I do? – Kousha Oct 31 '14 at 01:49
  • @Kousha You't not trying to create a method, you're trying to create a static constant. You can't do that with facades. Just put the data somewhere else. – Alana Storm Oct 31 '14 at 05:12