1

In Laravel's illuminate/support/Facades/Facade.php file there's a following method:

/**
 * Get the registered name of the component.
 *
 * @return string
 *
 * @throws \RuntimeException
 */
protected static function getFacadeAccessor()
{
    throw new RuntimeException('Facade does not implement getFacadeAccessor method.');
}

Is there any potential of doing that instead of just defining an abstract method, like below?

abstract protected static function getFacadeAccessor();

Why did they possibly want to reinvent the wheel?

Robo Robok
  • 21,132
  • 17
  • 68
  • 126
  • `abstract` methods require implementation in child classes. Implementation of `getFacadeAccessor` is likely optional, which means `abstract` would be the wrong keyword to use. – Bytewave Sep 03 '17 at 17:16
  • Dis you read entire topic? – Robo Robok Sep 03 '17 at 18:35
  • Yes. If you extend an `abstract` class, you **must** provide implementations for **all** `abstract` methods in that class. However, I'm assuming that providing an implementation for `getFacadeAccessor` is **optional**, so using an `abstract` method would make anyone defining a Facade provide some implementation for `getFacadeAccessor` even if they don't want/need to. So, leaving this implementation instead means programmers aren't required to define the method themselves when making a Facade. – Bytewave Sep 03 '17 at 18:40
  • You didn't read the topic then. Take a look at the snippet. How is implementation of that method optional, if it throws the exception telling you to implement it? xD – Robo Robok Sep 03 '17 at 18:42
  • 1
    Because you don't understand I mean. It's optional in the sense that I don't have to write a class that then provides an implementation for it. If it were `abstract`, I'd have to provide one, even if I don't want to. I'd probably end up writing an implementation like you see above, but in my child class. So Laravel just handles that for you, and leaves it up to you if you actually want to provide an implementation instead. – Bytewave Sep 03 '17 at 18:47
  • But facade without this method doesn't make any sense. – Robo Robok Nov 23 '18 at 23:48

1 Answers1

0

I found the following reason here:

This method is designed to be overridden when extending the Facade class to return a string, the key which the service represented by the facade is bound within the container. By default, it throws an exception if not implemented. This gives a more informative message to those creating custom facades than if the framework were to instead use an abstract method.

Hatzegopteryx
  • 600
  • 6
  • 13
  • Nice find! It's rather stupid though. Laravel is not for dummies, I'm pretty sure most developers would find a regular solution more clear than this quirky idea. – Robo Robok Nov 23 '18 at 23:48