atphp / common

Common tools for PHP
https://packagist.org/packages/atphp/common
2 stars 0 forks source link

Add LazyGetSetTrait #34

Open and1truong opened 10 years ago

and1truong commented 10 years ago
<?php

namespace MyProject {
  class Person {
    use LazyGetSetTrait;

    /** @var string */
    private $name;

    /** @var Person */
    private $father;
  }
}

$pa = new MyProject\Person();
$pa->setName('Pa be Quyen');

$person = new MyProject\Person();
$person->setName('Quyen Dap Chai');
$person->setFather($pa); // ok
$person->setFather('Pa be Quyen'); // Unexpected…Exception

echo $person->getName(); // Quyen Dap Chai
echo $person->getFather->getName(); // Pa be Quyen

Some notes

  1. fullName -> methods are getFullName, setFullName
  2. provide lazy setter: propertyName -> setPropertyName()
  3. provide lazy getter: propertyName -> getPropertyName()
  4. provide lazy checker: propertyName -> hasPropertyName()
  5. lazy setter have to validate all scalar type-hint (bool, number, string, …)
  6. lazy setter have to validate object, interface type-hint.
and1truong commented 10 years ago

Thank you @quyenbui for first draft

and1truong commented 10 years ago

Should check https://github.com/mnapoli/PhpDocReader