Skip to content

Instantly share code, notes, and snippets.

@ziadoz
Last active February 23, 2024 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ziadoz/d42c87fe27930cbd15f263a4bb7a45fe to your computer and use it in GitHub Desktop.
Save ziadoz/d42c87fe27930cbd15f263a4bb7a45fe to your computer and use it in GitHub Desktop.
Hypothetical PHP Getter/Setter Attribute Syntax
<?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