Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional abstract method

Tags:

I currently have an abstract class which i am extending to other controllers. I have a abstract function within the abstract class which takes the value and places it in the __construct.

abstract class Controller extends BaseController {
    abstract public function something();

    public function __construct(Request $request) {
        if (!is_null($this->something())){
            $this->global_constructor_usse = $this->something();
        }
    }
}

My problem is that on controllers that dont require this abstract function, i am having to place in the empty function.

class ControllerExample extends Controller {
  public function something(){
      return 'somethinghere';
  }
}

Is there anyway to making the abstract function optional or have a default value?

class EmptyControllerExample extends Controller {
  public function something(){}
}

Or what would be the best method of going about this?

like image 606
Sophie Rhodes Avatar asked Nov 09 '16 11:11

Sophie Rhodes


People also ask

How do you write an optional abstract method?

It is not possible to have a abstract method optional, as it is implied in PHP that all abstract methods must have an implementation. There are legit use cases for optional abstract methods, yes: event handlers, metadata describers, etc.

Is abstract optional?

In an abstract interface keyword, is optional for declaring a method as an abstract. In an abstract class, the abstract keyword is compulsory for declaring a method as an abstract. An interface can have only public abstract methods.

Is optional class an abstract class?

Abstract methods defined in classes cannot be marked as optional.

How can we override an abstract class method?

We cannot create objects of an abstract class. To implement features of an abstract class, we inherit subclasses from it and create objects of the subclass. A subclass must override all abstract methods of an abstract class. However, if the subclass is declared abstract, it's not mandatory to override abstract methods.


2 Answers

It is not possible to have a abstract method optional, as it is implied in PHP that all abstract methods must have an implementation.

There are legit use cases for optional abstract methods, yes: event handlers, metadata describers, etc. Unfortunately, you'll need to use regular, non-abstract methods with an empty body, and indicate in PHPDoc that they will do nothing unless extended.

Be wary, though: this can very quickly turn into code smell by diffusing a class responsability with their children. If you're dealing with generic events, you can look into Laravel's own event system, or the Observer pattern instead.

like image 98
DfKimera Avatar answered Sep 28 '22 13:09

DfKimera


Abstract functions in a parent class, should only be used if its required by your application to implement the following method in all controllers who inherits from it, clearly it is not the case.

In this case i would make a trait. Here you create a trait which can be implemented by the classes who needs it. Notice the use keyword usage, use somethingTrait;

trait somethingTrait
{
    public function something()
    {
        echo "something called";
    }
}

class Controller
{
    use somethingTrait;

    public function run()
    {
        $this->something();
    }
}

phpfiddle link

Another aproach could be doing a class inheritance structure, if the controllers you want to implement the methods has something in common. Where you would implement your special method in CrmController, where you still would be able to create shared methods in the abstract controller.

                                 AbstractController
                                         |
                                    CrmController
                                         |
                                   CompanyController

For your question, 'Is there anyway to making the abstract function optional or have a default value?' No, and you are down the wrong path if you are trying to make abstract function optional. Hope my suggestions can help.

like image 43
mrhn Avatar answered Sep 28 '22 13:09

mrhn