From:             glideraerobatics at hotmail dot com
Operating system: 
PHP version:      5.3.0alpha2
PHP Bug Type:     Feature/Change Request
Bug description:  readonly attribute for (public) class variables

Description:
------------
Here is a description of this feature in C#:
http://blog.paranoidferret.com/index.php/2007/09/12/csharp-tutorial-the-readonly-keyword/

In a nutshell it allows you to create classes with public variables that
are readonly and can only be written to / initialized by the contructor of
the class itself.

This allows the creation of simple objects without having using accessor
methods to protect the internal data that was validated and initialized
during construction from corruption.

Note: bug http://bugs.php.net/bug.php?id=39467 is about a similar problem
but the proposed solution is not quite right.

Reproduce code:
---------------
class Person
{
  public readonly $name = null;
  public readonly $age = null;
  public readonly $weight = null;

  public function __construct($name, $age, $weight) {
  {
    if (!isAgeToWeightRatioSane($age, $weight)) {
      throw new InvalidArgumentException("Invalid age to weight ratio:
$age : $weight");
    }
    // TODO: other sanity checks here.
    $this->name = $name;
    $this->age = $age;
    $this->weight = $weight;
  }
}


$person = new Person('Joe', 22, 100);

$person->age = 33; // throws a yet to be named exception




-- 
Edit bug report at http://bugs.php.net/?id=46506&edit=1
-- 
Try a CVS snapshot (PHP 5.2):        
http://bugs.php.net/fix.php?id=46506&r=trysnapshot52
Try a CVS snapshot (PHP 5.3):        
http://bugs.php.net/fix.php?id=46506&r=trysnapshot53
Try a CVS snapshot (PHP 6.0):        
http://bugs.php.net/fix.php?id=46506&r=trysnapshot60
Fixed in CVS:                        
http://bugs.php.net/fix.php?id=46506&r=fixedcvs
Fixed in CVS and need be documented: 
http://bugs.php.net/fix.php?id=46506&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=46506&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=46506&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=46506&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=46506&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=46506&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=46506&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=46506&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=46506&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=46506&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=46506&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=46506&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=46506&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=46506&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=46506&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=46506&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=46506&r=mysqlcfg

Reply via email to