1

クラス構造にメソッドチェーンを使用しています。

だから私の問題は、ある関数でエラーが発生したときにどうすればチェーンを壊すことができるかということです。

以下は私のコードです:

 <?php
    class demo
    {
        public __construct()
        {
            $this->a='a';
            $this->b='b';
            $this->error = false;
        }

        public function demo1()
        {
            // Some operation here
            // Now based on that operation

            if(Operation success)
            {
                return $this;
            }
            else
            {
                // What should i write here which break the chain of methods.
                // It will not execute the second function demo2
            }
        }

        public function demo2()
        {
            // Some operation here
            // After function Demo1
        }

    }

    $demoClass = new demo();

    $demoClass->demo1()->demo2();

?>

編集:

$demoClass = new demo();

    try
    {
        $demoClass->demo1()->demo2();
    }
    catch(Exception $e)
    {
        echo "Caught Exception:->".$e->getMessage();
    }   

ありがとう

Avinash

4

1 に答える 1

3

そこでユーザー例外をスローする必要があると思います。

        if(Operation success)
        {
            return $this;
        }
        else
        {
            // What should i write here which break the chain of methods.
            // It will not execute the second function demo2

            throw new Exception('error');
        }
于 2010-02-08T05:18:16.977 に答える