note 48224 added to language.oop5.constants
| From: | caliban at darklock dot com | Date: | Wed, 15 Dec 2004 18:55:32 +0000 |
| Subject: | note 48224 added to language.oop5.constants | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-81828@lists.php.net to get a copy of this message | ||
Lest anyone think this is somehow an omission in PHP, there is simply no point to having a protected
or private constant. Access specifiers identify who has the right to *change* members, not who has
the right to read them:
<?php
// define a test class
class Test
{
public static $open=2;
protected static $var=1;
private static $secret=3;
}
$classname="Test";
// reflect class information
$x=new ReflectionClass($classname);
$y=array();
foreach($x->GetStaticProperties() as $k=>$v)
$y[str_replace(chr(0),"@",$k)]=$v;
// define the variables to search for
$a=array("open","var","secret","nothing");
foreach($a as $b)
{
if(isset($y["$b"]))
echo "\"$b\" is public: {$y["$b"]}<br/>";
elseif(isset($y["@*@$b"]))
echo "\"$b\" is protected: {$y["@*@$b"]}<br/>";
elseif(isset($y["@$classname@$b"]))
echo "\"$b\" is private: {$y["@$classname@$b"]}<br/>";
else
echo "\"$b\" is not a static member of $classname<br/>";
}
?>
As you can see from the results of this code, the protected and private static members of Test are
still visible if you know where to look. The protection and privacy are applicable only on writing,
not reading -- and since nobody can write to a constant at all, assigning an access specifier to it
is just redundant.
----
Manual Page -- http://www.php.net/manual/en/language.oop5.constants.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+48224
Delete -- http://master.php.net/manage/user-notes.php?action=delete+48224&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+48224&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+48224&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+48224&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+48224&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+48224&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+48224&report=yes
Search -- http://master.php.net/manage/user-notes.php