2017 © Pedro Peláez
 

library accessors-trait

Trait for Property Accessors Generation

image

selikhovleonid/accessors-trait

Trait for Property Accessors Generation

  • Thursday, November 30, 2017
  • by selikhovleonid
  • Repository
  • 1 Watchers
  • 0 Stars
  • 6 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Accessors Trait

Trait for Property Accessors Generation, (*1)

Latest Unstable Version License PHP from Packagist, (*2)

Installing

The minimum required PHP version is PHP 5.4. You will need Composer dependency manager to install this tiny tool., (*3)

php composer.phar require selikhovleonid/accessors-trait

Quick start

When the trait is used in a child class, it catches the calls of undeclared methods of this class. If the name of the invoked method matches the setProperty, getProperty or isPropertySet pattern and the target class has corresponding property, then this trait calls needed accessor as if it was declared directly in the child class., (*4)

You need just add one of the following tags to the PHPDoc block to mark property as accessible to the corresponding methods: @get, @set, @isset. The @accessors annotation marks property as full-accessible., (*5)

/**
 * Foo class description
 */
class Foo
{
    use \nadir2\tools\AccessorsTrait;

    /**
     * @var string Property description
     * @accessors
     */
    protected $property;

    /**
     * @var array Another property description
     * @get
     * @isset
     */
    private $anotherProperty = [];

    /**
     * This method sets value of 'another property'.
     * @param array $data Passed data.
     * @return self
     */
    public function setAnotherProperty(array $data)
    {
        $this->anotherProperty = $data;
        return $this;
    }
}

$foo = new Foo();

// The following code is valid
if (!$foo->isPropertySet()) {
    $foo->setProperty('bar');
    $bar = $foo->getProperty();
}
if (empty($foo->getAnotherProperty())) {
    $baz = $foo->setAnotherProperty(['baz'])->getAnotherProperty();
}

The Versions

30/11 2017

dev-master

9999999-dev

Trait for Property Accessors Generation

  Sources   Download

MIT

The Requires

 

by Leonid Selikhov

getter setter property accessors