GetSetGo-Php Dynamic Setter Getter Library
GetSetGo is a dynamic setter-getter library for PHP 5.4+
. Its developed by me and Muhammed Usman, and yes, it’s well unit tested.
You can use methods like setFoo('bar')
and getFoo()
, which you don’t have to create (in your class). GetSetGo
will make these methods work for you automatically as long as you have a $foo
property in your class.
It makes use of Traits, so using it is super simple, you don’t have to extend any class, as you can extend a single class only, we don’t force you to use ours. You can restrict to only getter
or setter
or you can specify a Type for property using annotations
.
Installation
GetSetGo
uses Composer to make it easy to use. If you don’t know how to use composer then learn it and add the following line in your composer.json
file in the require section :
"require": { "usmanhalalit/get-set-go": "dev-master" }
Usage
Just add use \GetSetGo\SetterGetter
in your classes to make it available in the classes where you want to use it, for example :
class User { use \GetSetGo\SetterGetter; protected $name; // your class logic }
Now you can use it as follows :
$user = new User; $user->setName('Heera'); echo $myClass->getName();
This is the basic use of this.
Restrict Getter or Setter or Both
You can use annotation in you class property if you want to disable setter, getter or both.
For a readOnly property you can restrict the Setter
/** * We can't use setSomeProperty() anymore. * * @var * @setter false */ protected $someProperty;
Set the Getter
restricted
/** * We can't use getSomeProperty() anymore. * * @var \stdClass * @getter false */ protected $someProperty;
Restrict both if you want
/** * We can't use setSomeProperty() or getSomeProperty(). * * @getter false * @setter false */ protected $someProperty;
Type Casting
/** * Should be an instance of stdClass only. * * @var \stdClass */ protected $shouldBeStdClass;
It has to be an array
in the given example
/** * Should be an array only. * * @var Array */ protected $shouldBeArray;
It has to be an String
in the given example
/** * Should be a string only * * @var String */ protected $shouldBeArray;
It has to be a Number
in the given example
/** * Should be a number only. * * @var Number */ protected $shouldBeNumber;
You can also use
/** * Should be an object only. * * @var Object */ protected $shouldBeObject;
GetSetGo
assumes that you use proper camelCase
so name your properties like $pdoInstance
not $PDOInstance
and call setPdoInstance()
method.