> -----Original Message----- > From: leledumbo [mailto:leledumbo_cool@xxxxxxxxxxx] > Sent: 03 February 2009 05:03 > To: php-general@xxxxxxxxxxxxx > Subject: Visibility of class constant > > > I got a weird behaviour of class constant. Suppose I have Index_Controller > and Another_Controller classes, both extending Controller class. I define > some constants (let's assume I only have one, call it MY_CONST) in > Controller class to be used by its descendants. In Index_Controller, I can > freely use MY_CONST without parent:: needed. However, this isn't the case > with Another_Controller. Without parent:: I got notice Use of undefined > constant MY_CONST - assumed 'MY_CONST'. How could this happen and what's the > correct behaviour? It's actually nicer to have it without parent::, assuming > that constants have protected visibility specifier (which isn't possible > AFAIK :-( ). You cannot access a class constant just by the constant name. See http://docs.php.net/manual/en/language.oop5.paamayim-nekudotayim.php. you need to use self::MY_CONST, I guess; your code might make it clearer. e.g. class Controller { const CONSTANT = 'foo<br />'; } class Index_Controller extends Controller { public function __construct() { echo CONSTANT; echo '<br/>'; // gives warning echo self::CONSTANT; // foo echo parent::CONSTANT; // foo } } class Another_Controller extends Controller { const CONSTANT = 'bar<br />'; public function __construct() { echo self::CONSTANT; // bar echo parent::CONSTANT; // foo } } new Index_Controller would give you the warning you described for CONSTANT and return 'foo' for self::CONSTANT and parent::CONSTANT as CONSTANT was inherited. In Another_Controller CONSTANT is overridden so self::CONSTANT would be 'bar' and parent::CONSTANT would be 'foo'. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php