0

私のphpコードについて助けが必要です。これは私のコードです

class myclass extends anotherclass {

    var $bAlert;

    function myclass($var) {
        parent::anotherclass($var);
        $this->bAlert = false;
    }

function alert() {
        $this->bAlert = 'true';
        return;
    }

function display() {
        if(!$this->bAlert) {
           return;
        }
        return 'blah blah';
    }
}

今私が欲しいのは、display()が呼び出されたときに画面に何かを表示する機能がありますが、それは問題ではありませんが、alert()が呼び出された後にのみ表示することです。ここに私が考えたことがありますが、問題があります。alert() が呼び出されたら $bAlert の値を永久に true に変更したいのですが、もちろんそれは起こりません。それで、誰か他の素晴らしいアイデアやそれを行う他の方法を思いついた人はいますか?

ありがとう

4

2 に答える 2

2

シングルトン クラスを使用する

hereシングルトン クラスの詳細については、こちらをご覧ください

編集:

または、静的変数とメソッドを使用できます

編集:

このコードを参照してください:

<?php 
class myclass extends anotherclass {
    static public $bAlert;
    function myclass($var) {
        parent::anotherclass($var);
        self::$bAlert = false;
    }
    static function alert() {
        self::$bAlert = 'true';
        return;
    }
    function display() {
        if(!self::$bAlert) {
        return;
        }
        return 'blah blah';
    }
}
于 2012-11-03T08:22:34.153 に答える