https://github.com/spekkionu/property-access
Trait that automatically calls getters and setters for property access.
https://github.com/spekkionu/property-access
getters php setters
Last synced: 5 months ago
JSON representation
Trait that automatically calls getters and setters for property access.
- Host: GitHub
- URL: https://github.com/spekkionu/property-access
- Owner: spekkionu
- License: mit
- Created: 2015-04-02T01:15:10.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2017-12-23T04:59:56.000Z (over 8 years ago)
- Last Synced: 2025-08-11T21:45:48.982Z (11 months ago)
- Topics: getters, php, setters
- Language: PHP
- Size: 15.6 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Property Access Trait
[](https://travis-ci.org/spekkionu/property-access)
[](https://scrutinizer-ci.com/g/spekkionu/property-access/?branch=master)
[](https://scrutinizer-ci.com/g/spekkionu/property-access/?branch=master)
[](https://insight.sensiolabs.com/projects/f01daa7f-b46d-4575-aeb8-7be3885d4967)
Trait that automatically calls getters and setters for property access.
```php
use Spekkionu\PropertyAccess\PropertyAccessTrait;
class ExampleClass
{
use PropertyAccessTrait;
private $name;
private $email;
}
```
```php
$example = new ExampleClass();
$example->name = 'Bob';
$example->email = 'bob@example.com';
echo $example->name; // Bob
$example->fill(array(
'name' => 'Steve',
'email' => 'steve@example.com'
));
echo $example->email; // steve@example.com
```
## Getters and Setters will be called
### You can even use Value Objects
```php
use Spekkionu\PropertyAccess\PropertyAccessTrait;
class ExampleClass
{
use PropertyAccessTrait;
private $name;
private $email;
public function setEmail(EmailAddress $email){
$this->email = $email;
}
}
// Value Object
class EmailAddress
{
private $email;
public function __construct($email)
{
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new InvalidArgumentException('Not a valid email address.');
}
$this->email = $email;
}
public function getValue()
{
return $this->email;
}
public function __toString()
{
return $this->getValue();
}
}
// Usage
$example = new ExampleClass();
$example->email = new EmailAddress('bob@example.com');
```