From:             president at basnetworks dot net
Operating system: 
PHP version:      6SVN-2009-09-11 (SVN)
PHP Bug Type:     Feature/Change Request
Bug description:  C# style property get/set syntax

Description:
------------
I would like to request a C# style get/set syntax (called a property in
C#) for PHP.  Basically, it looks and acts like a class member/variable
from outside the class, but it is actually a set of two methods.  It can be
used to provide only read or write access to a class member, do pre or post
processing on a member, or be completely dynamic like a set of class
methods.

A property contains two methods between braces, named get and set.  get
must always have a return statement, while set has a magic variable "value"
or "$value" which is the variable that was passed to the property.  Either
method can be omitted to make the property read-only or write-only.

The same effect can be achieved by creating a GetVar() and SetVar() method
to create a sudo-property "var", although it is by far much clumsier and
less intuitive.

I also realize the same effect received outside the class can be achieved
using the __get() and __set() methods, but these methods are only really
useful in a small instance of situations, like giving access to an internal
array as though each index is a property.  These magic methods are not at
all useful for using on an individual property basis, and it gets worse
when inheritance is introduced.

The C# syntax is as follows:

class TimePeriod
{
    private double seconds;

    public double Hours
    {
        get { return seconds / 3600; }
        set { seconds = value * 3600; }
    }
}



The PHP syntax would be similar to the following:

class TimePeriod
{
    private $seconds;

    public property Hours
    {
        get { return $this->seconds / 3600; }
        set { $this->seconds = $value * 3600; }
    }
}



You would use it exactly the same as a public class member:

$time = new TimePeriod();
$time->Hours = 24;
echo $time->Hours;



As opposed to the alternative:
$time = new TimePeriod();
$time->SetHours(24);
echo $time->GetHours();



Additionally, the get and set methods can have separate visibilities like
in the following example where get is public and set is protected:

public property Name
{
    get { return $this->name; }
    protected set { $this->name = $value; }
}



There is another ticket that is similar but not the same thing here:
http://bugs.php.net/bug.php?id=34194

It suggests separate getter/setter methods, which in my opinion are much
less intuitive.  I believe that following the C# format would help to keep
a standard format, and would be the least confusing.

The poster of that bug also fails to realize that separate visibility
levels can be achieved for properties using the C# syntax, as shown above.



The C# documentation on properties is available here:
http://msdn.microsoft.com/en-us/library/x9fsa0sw%28VS.80%29.aspx

The C# documentation on Asymmetric Accessor Accessibility for properties
is available here:
http://msdn.microsoft.com/en-us/library/75e8y5dd%28VS.80%29.aspx


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

Reply via email to