Edit report at https://bugs.php.net/bug.php?id=39467&edit=1
ID: 39467 Comment by: uramihsayibok at gmail dot com Reported by: kevin at metalaxe dot com Summary: Expaned Class members to allow setting of a read only value at runtime Status: Open Type: Feature/Change Request Package: Feature/Change Request Operating System: * PHP Version: 5.2.0 Block user comment: N Private report: N New Comment: If you need to define the "constant" in your constructor then what you have isn't a constant. So if you don't want it public then don't make it public. class parser { private $_magic_quotes = false; function __construct() { $this->_magic_quotes = /* etc */; } } Otherwise you have to define the constant outside the class and if that's the case then you might as well use define(). If you're really hung up on the class constant syntax then you can define("MAGIC_QUOTES", /* etc */); class parser { const MAGIC_QUOTES = MAGIC_QUOTES; } Side notes: 1. Though this isn't the place for discussion, if it were I'd talk about how a global constant for this particular example makes more sense anyways. 2. Since magic_quotes *and its functions* are going way the best way to check for it is (function_exists("get_magic_quotes_gpc") && get_magic_quotes_gpc()) Previous Comments: ------------------------------------------------------------------------ [2006-11-10 23:49:45] kevin at metalaxe dot com There is a typo in my example code above. public my_stripslashes( $str ) { if( self::magic_quotes === true ) { $str = stripslashes( $str ); } return $str; } should be public my_stripslashes( $str ) { if( $this->magic_quotes === true ) { $str = stripslashes( $str ); } return $str; } ------------------------------------------------------------------------ [2006-11-10 23:47:46] kevin at metalaxe dot com Description: ------------ Since http://bugs.php.net/bug.php?id=39466 was shot down, PHP is in need of a method to which we can set class member variables as read-only during runtime to prevent changing of values that are critically important for script execution. I would like to use a class constant for this but, because it cannot be assigned a value at runtime, it is impossible to do so. The reproduce code is an example of a value that I would like to set as read only. The scope of the variable should not matter in this suggestion as it is practical that it would need to be changed given a proper reason. Reproduce code: --------------- <?php class parser { public $magic_quotes = false; public __construct() { //Add some sort of identifier here to set the //value read only (readonly)$this->magic_quotes = (bool)get_magic_quotes_gpc(); } public my_stripslashes( $str ) { if( self::magic_quotes === true ) { $str = stripslashes( $str ); } return $str; } public change_magic_quotes($to) { $this->magic_quotes = $to; } } $parser = new parser(); //Returns stripped string $stripped = $parser->my_stripslashes( 'Hi, there\'s a coke in the fridge' ); //Doesn't change the value of magic_quotes and flags an //E_WARNING or E_NOTICE error. $parser->change_magic_quotes( true ); ?> Expected result: ---------------- Hope to have it not allow the variable to be changed (ha, a constant, what a silly notion) and pop an error of some kind to the parser. Actual result: -------------- Not implemented, thus values can be changed at any time as long as the variable is within the visibility scope of calling party. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=39467&edit=1