note 53935 added to language.oop5.constants
| From: | php-general at lists dot php dot net | Date: | Fri, 17 Jun 2005 16:29:01 +0000 |
| Subject: | note 53935 added to language.oop5.constants | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-90588@lists.php.net to get a copy of this message | ||
It's important to note that constants cannot be overridden by an extended class, if you with to
use them in virtual functions. For example :
<?php
class abc
{
const avar = "abc's";
function show()
{
echo self::avar . "\r\n";
}
};
class def extends abc
{
const avar = "def's";
function showmore ()
{
echo self::avar . "\r\n";
$this->show();
}
};
$bob = new def();
$bob->showmore();
?>
Will display:
def's
abc's
However, if you use variables instead the output is different, such as:
<?php
class abc
{
protected $avar = "abc's";
function show()
{
echo $this->avar . "\r\n";
}
};
class def extends abc
{
protected $avar = "def's";
function showmore ()
{
echo $this->avar . "\r\n";
$this->show();
}
};
$bob = new def();
$bob->showmore();
?>
Will output:
def's
def's
----
Manual Page -- http://www.php.net/manual/en/language.oop5.constants.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+53935
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+53935&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+53935&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+53935&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+53935&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+53935&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+53935&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+53935&report=yes
Search -- http://master.php.net/manage/user-notes.php