如果我有一類結構的值可以是真或假,這並沒有改變,目前作爲變量來實現倒不如將其更改爲常量,比如:使用類常量和壓倒一切的PHP
class Parent {
const BOOL_CONST = false;
...
}
class SomeChild extends Parent {
const BOOL_CONST = true;
...
}
後來我有一個對象,其可以是任何類型在類層次結構中,無論是父或它的一個子,以及部分孩子可能,像「SomeChild」重載值設定爲真。
有一些方法可以讓我訪問常量不知道類?換句話說,我可以這樣做:
$object->BOOL_CONST
或者是否最好將這些值作爲變量,即使它們確實不應該改變?
UPDATE
我已經改寫上面我的問題,以更好地表達我試圖問。
可能重複: //stackoverflow.com/questions/956401/can-i-get-consts-defined-on-a-php-class) – 2012-03-30 15:12:52
@JuicyScripter自己看不到相似性?我想我的問題更多地涉及需要在對象級別獲得不應該改變的值,但不知道類名。目前這些都是作爲變量來實現的,但我認爲它們應該是常量。但看起來這樣做會破壞很多東西。 – AntonChanning 2012-03-30 15:34:59