RE: [PHP-DEV] Re: Patch: getters/setters syntax Implementation

From: Date: Tue, 06 Dec 2011 03:23:52 +0000
Subject: RE: [PHP-DEV] Re: Patch: getters/setters syntax Implementation
References: 1  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
I believe the attempt with the RFC was to mimic the syntax that C# went with, the RFC is here: https://wiki.php.net/rfc/propertygetsetsyntax

The first public would apply to either of the get/set which did not have it further defined, for
example:

public $Hours {
	get { ... }
	private set { ... }
}

Also, with automatic implementations (at present) the parent access level controls the automatic
property generation:

private $Hours {
	public get;
	public set;
}

Would define an internal variable of $__Hours as private.  Perhaps it should always be a private
internal variable, it was not designated in the rfc and I made it this way to be the most flexible
for the author.

-----Original Message-----
From: Rasmus Schultz [mailto:[email protected]] 
Sent: Monday, December 05, 2011 5:11 PM
To: [email protected]
Subject: [PHP-DEV] Re: Patch: getters/setters syntax Implementation

2011/12/4 Clint M Priest <[email protected]>:
> Updated patch w/o white-space:
http://www.clintpriest.com/patches/accessors_v1.patch
>
> In the end it is a relatively simple patch.  The new syntax 
> effectively
creates internal functions on the object and the system looks for those functions and calls them at
the appropriate time.
>
> Example:
> class z {
>        public $Hours {
>                        public get { return $this->_Hours; }
>                        protected set { $this->_Hours = $value; }
>        }
> }
>
> Defines:
> $o->__getHours();
> $o->__setHours($value);

You forgot to declare the backing field z::$_Hours in this example.

From a semantic point of view, I find it misleading to first declare $Hours as public, then lowering
the bar by making the set-accessor protected.

The most common use-case for accessors is public - so I would suggest a syntax more along the lines
of this:

class z {
  private $_hours;

  get $hours {  // accessor is public by default
    return $this->_hours;
  }

  protected set $hours {
    $this->_hours = $hours; // local variable $hours is the new value
  }
}

And perhaps a short form for added convenience, where the backing-field is automatically added for
you - e.g.:

class z {
  get $hours {  // accessor is public by default
    return $value; // $value provides access to the backing field (same way $this provides access to
the object context)
  }

  protected set $hours {
    $value = $hours; // local variable $hours is the new value, $value references the backing field
  }
}

thoughts?


Thread (23 messages)

« previous php.internals (#56787) next »