, (*1)
Piano Accessor
This package allows us to create getters and setters just by using a few annotations., (*2)
Installing
composer require piano/accessor
Usage example
See the example:, (*3)
This User class:, (*4)
<?php
namespace App;
class User
{
private $name;
private $age;
private $createdAt;
public function setName($name)
{
$this->name = $name;
}
public function setAge($age)
{
$this->age = (int) $age;
}
public function setCreatedAt(\DateTime $createdAt)
{
$this->createdAt = $createdAt;
}
public function getName()
{
return $this->name;
}
public function getAge()
{
return (int) $this->age;
}
public function getCreatedAt()
{
return $this->createdAt;
}
}
Is the same as this User class:, (*5)
<?php
namespace App;
class User
{
use \Piano\AccessorTrait;
/**
* @set
* @get
*/
private $name;
/**
* @set int
* @get int
*/
private $age;
/**
* @set \DateTime
* @get
*/
private $createdAt;
}
As you can see it's possible to specify the type hint or type cast when defining the @set and it's also possible to specify the type cast when defining the @get.
That's optional though., (*6)
As below:, (*7)
| Setting |
Getting |
| @set int |
@get int |
| @set integer |
@get integer |
| @set bool |
@get bool |
| @set boolean |
@get boolean |
| @set float |
@get float |
| @set double |
@get double |
| @set string |
@get string |
| @set array |
@get array |
| @set object |
@get object |
For @set any other value will be treated as type hint., (*8)