jeudi 11 octobre 2018

PHP force class to have a certain method if class variable has been set

I have a simple requirement where I want to allow a Class (Laravel Model) to define whether a controller should check whether it's allowed to be deleted or not based on a class variable like so

public $verify_delete = true;

Currently if this variable is set I must add the following method;

public function deletable()
{
    $deletable = true;

    if ($this->has_children()) {
        $deletable = false;
    }

    // ...

    return $deletable;
}

The first variable is simply an indication the code running this class should also run the deletable() method.

So what I want is a way to force a developer to implement the deletable() method if they set the $verify_delete to true, and throw an error if they have not done so. I don't know much about traits and implements at this stage, so I'm not sure if they're the right route to go down.

If this is not the best method to achieve what I'm after I'm all ears.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire