PHPackages                             rakshazi/get-set-trait - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. rakshazi/get-set-trait

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

rakshazi/get-set-trait
======================

Dynamic Setter-Getter for PHP 5.4+

1.0.5(6y ago)411.5k↑16.7%21MITPHPPHP &gt;=5.4.0

Since May 27Pushed 6y ago2 watchersCompare

[ Source](https://github.com/rakshazi/getSetTrait)[ Packagist](https://packagist.org/packages/rakshazi/get-set-trait)[ Docs](https://github.com/rakshazi/getSetTrait)[ RSS](/packages/rakshazi-get-set-trait/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)DependenciesVersions (5)Used By (1)

GetSetTrait
===========

[](#getsettrait)

A dynamic setter-getter library for PHP 5.4+.

You can use methods like `setFoo('bar')` and `getFoo()`, which you DON'T have to create (in your class). GetSetTrait 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 only or you can specify a Type for property using **annotations**.

Installation
------------

[](#installation)

GetSetTrait uses [Composer](http://getcomposer.org/) to make hassles Go.

Learn to use composer and add this to require (in your composer.json):

> "rakshazi/get-set-trait": "@stable"

Library on [Packagist](https://packagist.org/packages/rakshazi/get-set-trait).

Usage
-----

[](#usage)

Just add this in your classes:

> use Rakshazi\\GetSetTrait;

```
class Dummy
{
    //Add ability to use dynamic getters and setters
    use \Rakshazi\GetSetTrait;
}

//Init dummy class
$dummy = new Dummy;
//Set new var 'message_for_world' with value
$dummy->setMessageForWorld('Hello');
//Will return "Hello\n"
echo $dummy->getMessageForWorld()."\n";
//Will return the same text
echo $dummy->getData('message_for_world')."\n";
//Set new message for our world!
$dummy->setData('message_for_world', 'Bye-bye!');
//Will return "Bye-bye!\n"
echo $dummy->getData('message_for_world')."\n";
//Will set new var 'new_message'
$dummy->setNewMessage('Use me now!');
//Will return true, value exists
$dummy->hasNewMessage();
//Will return false
$dummy->hasSomeOtherValue();
//Will show all object data
var_dump($dummy);
```

**That's basically it.**

Advanced usage
--------------

[](#advanced-usage)

### Data property

[](#data-property)

If you want save all data in `$object->someProperty` array instead of saving each property as object's property (`$object->property_name`), you can use `setDataProperty('data')` function, example:

```
