note 71077 deleted from language.oop5.constants by danbrown
| From: | danbrown@php.net | Date: | Wed, 03 Aug 2011 13:29:53 +0000 |
| Subject: | note 71077 deleted from language.oop5.constants by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-182179@lists.php.net to get a copy of this message | ||
Note Submitter: kevin at metalaxe dot com
----
In response to anon on 31-May-2006 02:03:
If you can define a global constant based on the return of a function, please explain why you do not
see it justifiable that a class constant should be able to be assigned in the same manner.
This affects ALL members of a class, not just constants and, for the sake of class constants, I see
this as quite severe limitation to PHP. Take the following class for example:
<?php
class parser
{
private $magic_quotes = false;
public function __construct()
{
$this->magic_quotes = (bool)get_magic_quotes_gpc();
}
public my_stripslashes( $str )
{
if( $this->magic_quotes )
{
$str = stripslashes( $str );
}
return $str;
}
}
?>
Wouldn't this be much cleaner and easier to impliment like this?
<?php
class parser
{
const magic_quotes = (bool)get_magic_quotes_gpc();
public my_stripslashes( $str )
{
if( self::magic_quotes_gpc )
{
$str = stripslashes( $str );
}
return $str;
}
}
?>
In the second iteration, there isn't a need to scour through the script to find where
magic_quotes is assigned a value and there is no confusion as to EXACTLY what magic_quotes is. More
so, there is no worry that some silly end user will be able to change the value of magic_quotes in
any of their "modifications" to your code as constants are a read only property.
Personally I find this practical for script operation., but completely impossible in PHP.