2012-03-30 68 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