V
V
Vitaly the Wise2017-04-11 13:44:39
PHP
Vitaly the Wise, 2017-04-11 13:44:39

Is it possible to stop execution of further class methods in the constructor?

Code example:
$obj = new Class();
$obj->method_1()->method_2()->...method_n();
Let's assume that checks passed in the constructor that do not suit us and we do not need further execution of class methods. Is it possible somehow not to start executing further class methods?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anton Shamanov, 2017-04-11
@SilenceOfWinter

Usually an exception is thrown. Make a boolean property of the class and set it in the constructor, and in the methods, perform checks.

M
Maxim Fedorov, 2017-04-11
@qonand

Is it possible to stop execution of further class methods in the constructor?

Do you want that just class methods are not processed? this cannot be done, and this violates the LSP principle. If for some reason it is not possible to complete the work in the class, throw an Exception, because in such cases, the code using the class must decide what to do

M
MrTimon, 2017-04-11
@MrTimon

Well, you can’t just cancel the execution of classes, although you can make one crutch.
You can write a simple dummy class

class Blank {
      public function __call($name, $arguments) {      
          return this;     
      }
}

and then in the constructor or in one of the methods to return an object of this class when you are not satisfied with the conditions. And then all subsequent methods will simply return an object of this class. Crutch of course but it should work)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question