Last active
February 23, 2024 13:26
-
-
Save ziadoz/d42c87fe27930cbd15f263a4bb7a45fe to your computer and use it in GitHub Desktop.
Hypothetical PHP Getter/Setter Attribute Syntax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Example | |
{ | |
// By default the attribute binds a getter to `getName()`and setter to `setName()`. | |
#[Accessor] | |
protected string $name; | |
// Custom getter and setter method names can be passed. | |
#[Accessor(getter: 'customGetter, setter: 'customSetter')] | |
protected string $name; | |
// A separate getter attribute works the same way... | |
#[Getter] | |
protected string $name; | |
#[Getter('customGetter')] | |
protected string $name; | |
// And a separate setter attribute works the same way too... | |
#[Setter] | |
protected string $name; | |
#[Setter('customSetter')] | |
protected string $name; | |
// And the actual methods... | |
public function getName(): string | |
{ | |
return ucwords($this->name); | |
} | |
public function setName(string $value): void | |
{ | |
$this->name = $value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment