php-parser icon indicating copy to clipboard operation
php-parser copied to clipboard

8.4: property hooks

Open genintho opened this issue 1 year ago • 0 comments

  • [x] get - simple
    public string $email {
       get => 'mailto:' . $this->email;
    }
  • [x] get - block
       get {
           'mailto:' . $this->email;
        }
  • [x] set - full block form
set ($value) { $this->[propertyName = $value }
  • [x] block form with implicit $value
set { $this->[propertyName = $value }
  • [x] expression form with explicit $value
set ($value) => {expression}
  • [x] expression form with implicit $value
set => {expression}// expression form with implicit $value
  • [x] get by reference
&get => { }
  • [x] default value
public string $role = 'anonymous' {
    set {
        Roles::from($value);
        $this->role = $value;
    }
}
  • [x] final on hooks
class StandardUser
{
    public string $email {
        final set {
           if (! filter_var($value, FILTER_VALIDATE_EMAIL, FILTER_FLAG_EMAIL_UNICODE)) {
               throw new InvalidArgumentException('Invalid email');
           }
           $this->email = $value;
        }
    }
}
  • [x] final on property
class User 
{
    // Child classes may not add hooks of any kind to this property.
    public final string $name;
 
    // Child classes may not add any hooks or override set,
    // but this set will still apply.
    public final string $username {
        set => [strtolower](http://www.php.net/strtolower)($value);
    }
}
  • [x] abstract
abstract class StandardUser
{
    abstract public string $email { get; }
}
  • [x] interface - get
interface I
{
    // An implementing class MUST have a publicly-readable property,
    // but whether or not it's publicly settable is unrestricted.
    public string $readable { get; }
}
  • [x] interface - set
interface I
{ 
    // An implementing class MUST have a publicly-writeable property,
    // but whether or not it's publicly readable is unrestricted.
    public string $writeable { set; }
}
  • [x] interface - get + set
interface I
{
    // An implementing class MUST have a property that is both publicly
    // readable and publicly writeable.
    public string $both { get; set; }
}

genintho avatar Nov 25 '24 05:11 genintho