2012-03-30 25 views
3

如果我有一類結構的值可以是真或假,這並沒有改變,目前作爲變量來實現倒不如將其更改爲常量,比如:使用類常量和壓倒一切的PHP

class Parent { 
    const BOOL_CONST = false; 

    ... 
} 

class SomeChild extends Parent { 
    const BOOL_CONST = true; 

    ... 
} 

後來我有一個對象,其可以是任何類型在類層次結構中,無論是父或它的一個子,以及部分孩子可能,像「SomeChild」重載值設定爲真。

有一些方法可以讓我訪問常量不知道類?換句話說,我可以這樣做:

$object->BOOL_CONST 

或者是否最好將這些值作爲變量,即使它們確實不應該改變?

UPDATE

我已經改寫上面我的問題,以更好地表達我試圖問。

+0

可能重複: //stackoverflow.com/questions/956401/can-i-get-consts-defined-on-a-php-class) – 2012-03-30 15:12:52

+0

@JuicyScripter自己看不到相似性?我想我的問題更多地涉及需要在對象級別獲得不應該改變的值,但不知道類名。目前這些都是作爲變量來實現的,但我認爲它們應該是常量。但看起來這樣做會破壞很多東西。 – AntonChanning 2012-03-30 15:34:59

回答

1

PHP 5.3現在接受該對象作爲類參考:現在接受$this::BOOL_CONST

// 
// http://php.net/manual/en/language.oop5.constants.php 
// 
// As of PHP 5.3.0, it's possible to 
// reference the class using a variable. 
// The variable's value can not be a keyword 
// (e.g. self, parent and static). 
// 

// I renamed "Parent" class name to "constantes" 
// because the classname "Parent" can be confused with "parent::" scope 
class constantes 
{ 
    const test      = false; 
} 

// I renamed "SomeChild" too, with no reason... 
class OverloadConst extends constantes 
{ 
    const test      = true; 
    public function waysToGetTheConstant() 
    { 
     var_dump(array('$this'=>$this::test)); // true, also usable outside the class 
     var_dump(array('self::'=>self::test)); // true, only usable inside the class 
     var_dump(array('parent::'=>parent::test)); // false, only usable inside the class 
     var_dump(array('static::'=>static::test)); // true, should be in class's static methods, see http://php.net/manual/en/language.oop5.late-static-bindings.php 
    } 
} 

// Classic way: use the class name 
var_dump(array('Using classname' => OverloadConst::test)); 

// PHP 5.3 way: use the object 
$object = new OverloadConst(); 
var_dump(array('Using object'  => $object::test)); 
$object->waysToGetTheConstant(); 

請注意,您可以覆蓋類常量,而不是接口常量。 如果constantesOverloadConsts實現的接口,則不能覆蓋其常量test(或BOOL_CONST)。

來源

的[我能CONST對PHP類定義的?](HTTP
2

常量與雙冒號::

Parent::BOOL_CONST 

SomeChild::BOOL_CONST 

within the class 
parent::BOOL_CONST 
self::BOOL_CONST 
+0

+1謝謝。我想我應該把這些值作爲變量,儘管它們不應該改變。我需要他們從課外進行訪問。 – AntonChanning 2012-03-30 15:29:20

+0

正如你在上面看到的,它們是:'ClassName :: CONST' – 2012-03-30 15:59:32

+0

是的,但只有當我知道類名時纔有效。如果對象可以是一組類中的一個,我不能那麼做。 – AntonChanning 2012-03-30 18:28:03

1

不,你不能從一個對象上下文訪問常量,但你可以使用反射來搶班$對象,然後使用訪問::獲取BOOL_CONST。所以:

$class = get_class($object); 
$class::BOOL_CONST; 

好的,不,這不是技術上的反映。另外,如果$ class ::將正確解析,我並不是100%確定。如果上述不起作用,請使用實際的ReflectionClass類。

+0

+1雖然我懷疑使用ReflectionClass可能是爲我的目的矯枉過正。我最好將值保存爲變量,而不是僅僅因爲它們不應該改變而將它們整理爲常量。 – AntonChanning 2012-03-30 15:26:46

+0

我同意;通常更喜歡速度超過清潔(我經常這樣做),ReflectionClass是一個緩慢的巨獸。 – Matthematics 2012-03-30 17:28:59

1

你不能這樣做$object->BOOL_CONST,因爲類常量必須是靜態(SomeChild::BOOLCONSTANT)調用。

但是,也許你可以嘗試這樣的事情://編輯:該作品:)

$class = get_class($object); 
$const = $class::BOOL_CONST; 
7

有一些方法可以讓我訪問常量不知道類? 換句話說,我可以這樣做:

是,爲了引用一個常數,你將要使用以下結構:

  • 自:: NAME_OF_CONSTANT:給我一個常量在這個類中定義;如果我不定義它,從我的父母得到它
  • 靜:: NAME_OF_CONSTANT:給我在這個類中只定義了一個常數;從來不看我的父母吧
  • 父:: NAME_OF_CONSTANT:給我的只有我的父類中定義的常量;從來沒有看過自己

順便說一句,你使用的術語「重載」;不過,我相信你的意思是說「重寫」。在面嚮對象語言中,重載具有不同的語義含義。

+1

謝謝。你對我的「超載」和「壓倒一切」這兩個術語的闡述是正確的。我將編輯問題以使用正確的術語。 – AntonChanning 2012-04-02 11:02:18