This package allows us to create getters and setters just by using a few annotations.
composer require piano/accessor
See the example:
This User
class:
<?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:
<?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.
As below:
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.