-2

とても簡単な例をお見せしましょう...

class menu_r {

    public function anuncio(){
        return $a;
    }

    public function peido(){
        return $b;
    }

    public function running(){

    if(self::anuncio()){
        $retorno.= self::anuncio();
    }

    if(self::peido()){
        $retorno.= self::peido();
    }

    return print $retorno;
}

私は使用しています

$a = new menu_r();
$a->anuncio();
$a->running();

私の質問は、peido() が使用されていないため、peido() を表示しないようにするにはどうすればよいですか? 私はたくさん試しましたができませんでした。

4

1 に答える 1

0

これはそれを行う必要があります:

class menu_r {
    private $calledAnnuncio = false;
    private $calledPeido = false;

    public function anuncio() {
        $this->$calledAnnuncio = true;
        return $a;
    }

    public function peido() {
        $this->calledPeido = true;
        return $b;
    }

    public function running() {

        if ($this->calledAnnuncio) {
            $retorno.= self::anuncio();
        }

        if ($this->calledPeido) {
            $retorno.= self::peido();
        }

        return print $retorno;
    }

}

ここでは、それぞれのプライベート変数に「既に呼び出された特別な関数」状態を保存し、running() を呼び出すときに、これらの状態が true に設定されているかどうかを確認し、状態が true に設定されている場合にのみ、その関数を再度呼び出します。したがって、running() を呼び出す前に関数が直接呼び出された場合、running() 関数で再度呼び出されます。

于 2013-03-19T00:11:21.080 に答える