开发者

Is it bad practice?

Is bad practice to do like I show it below? I want to do next_function_to_do_smth function if there is rows in certain table and if it is, we call function and use die to close construct. If there isn't, we get rows from other table.

If it is bad practise, what should I use instead of it?

Thank you.

class test
{
    function __construct()
    {
        $this->get_mysql_count("mysql query to get COUNT(*) of smth");

        if($this->get_mysql_count > 0)
        {
            $this->next_function_to_do_smth();
            die();
        }

        $this->get_mysql_count(开发者_JAVA技巧"mysql query to get COUNT(*) of smth2");

        if($this->get_mysql_count > 0)
        {
            $this->next_function_to_do_smth2();
            die();
        }     

        //and so on, and so on   
    }
}


This not a very clean solution. I would consider a method initialize() which would be called from the constructor. The method would do the same as your constructor but will 'return' instead of 'die'. Or you could use an if / else if / else block to control the program execution flow.


Well, that is certainly strange tactics, but I don't think it is directly prohibited somewhere. Only the thing is that a class should not really execute die() construct. May be only very special classes like exception handlers should have this privilege.

May be, if you explain the whole thing, we will find something else to suggest.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜