开发者

Is there any way to stop object method execution from another method?

Situation is as follows:

There is a separate object that is responsible for generating page content, we can call it Content. Another object is its' parent - Core object. Content object calls Core method *Check_parameters_count($parameters_count)*, so Core has to check if given parameters count in the URI equals to the integer given while executing this method ($parameters_count), if not - Core should generate error page and stop executing Content's rendering method.

Is there any way I can do this without using if statement? Just using *$core->Check_parameters_count(2)* in the content class in order to simplify job of the specific rendering engine programmers.

Everything should look similar to this:

 class Core {
    public function Check_parameters_count($parameters_count) {
        if (count($this->parameters) != $parameters_count) {
            $this->Show_error(404);
            $this->Stop_executing($content, 'Render');
            //or
            stop($content->Render);
            //or something similar..
        }
    }
}

class Content {
    public function Render() {
        //check given parameters so we could know which page should be rendered and how many parameters should be given
        //...
        //lets say we should have exactly 2开发者_JAVA技巧 parameters
        $parameters_count = 2;
        //check it
        $core->Check_parameters_count($parameters_count);
        //if parameters count matched - continue method execution
        //...
    }
}


Throw an exception.

You have 3 scopes:

  • Function A (caller of function B)
  • Function B
  • Caller of function A

If you throw an exception in Function B, and make a try/catch block in caller of function A, function A execution will be interrupted by the exception. In fact - all code execution will be interrupted, up to a level which is enclosed in a corresponding try/catch block.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜