PHPackages                             snscripts/getset - 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. snscripts/getset

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

snscripts/getset
================

Trait for adding magic setters / getters to objects

1.0.0(8y ago)049.9k1MITPHPPHP &gt;=5.6.0

Since Jun 24Pushed 8y ago1 watchersCompare

[ Source](https://github.com/mikebarlow/GetSet)[ Packagist](https://packagist.org/packages/snscripts/getset)[ Docs](https://github.com/mikebarlow/GetSet)[ RSS](/packages/snscripts-getset/feed)WikiDiscussions master Synced 4d ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (1)

GetSet
======

[](#getset)

[![Author](https://camo.githubusercontent.com/9d903e7623152bba92e485dae02e0ffe50922554087701bab4ab685ea10150d2/687474703a2f2f696d672e736869656c64732e696f2f62616467652f617574686f722d406d696b656261726c6f772d7265642e7376673f7374796c653d666c61742d737175617265)](https://twitter.com/mikebarlow)[![Latest Version](https://camo.githubusercontent.com/efc0aba62243cbd96e231a2d79c88952525f2bd672b37ea544158d2c0a58952a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f6d696b656261726c6f772f6765747365742e7376673f7374796c653d666c61742d737175617265)](https://github.com/mikebarlow/GetSet/releases)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://github.com/mikebarlow/GetSet/blob/master/LICENSE)[![Build Status](https://camo.githubusercontent.com/b83800cbbe5a3ba6a2fc27afeee35ee99e842917be987d90ebadf766e35250d8/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6d696b656261726c6f772f4765745365742f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/mikebarlow/GetSet)

Introduction
------------

[](#introduction)

GetSet is a PSR-2 compliant trait to be used for adding magic getters / setters to objects.

Requirements
------------

[](#requirements)

### Composer

[](#composer)

GetSet requires the following:

- "php": "&gt;=5.6.0"

And the following if you wish to run in dev mode and run tests.

- "phpunit/phpunit": "~5.7"
- "squizlabs/php\_codesniffer": "~2.0"

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

[](#installation)

### Composer

[](#composer-1)

Simplest installation is via composer.

```
composer require snscripts/getset 1.*

```

or adding to your projects `composer.json` file.

```
{
    "require": {
        "snscripts/getset": "1.*"
    }
}

```

### Setup

[](#setup)

To initiate GetSet simply `use` the trait within your class

```
class MyClass
{
    use \Snscripts\GetSet\GetSet;

    ...
}

```

Usage
-----

[](#usage)

Once setup, simply set or get any variables on the object you need.

```
$MyClass->foo_bar = 'barfoo';

echo $MyClass->foo_bar; // barfoo

```

This will set the variable into a `$data` array added to your object via the GetSet trait.

### Variable Transformers

[](#variable-transformers)

You can create custom get / set transformers to change a variables data as it gets added to or retrieved from the `$data` variable. These should be in the format `setXAttr` and `getXAttr` where `X` is a CamelCased version of the variable you are setting or getting from the object.

In the example above, basic getter / setter methods for `foo_bar` could look like:

```
public function setFooBarAttr($value)
{
    $this->data['foo_bar'] = strtoupper($value);
}

public function getFooBarAttr()
{
    return strtolower($this->data['foo_bar']);
}

```

Mass data assignment
--------------------

[](#mass-data-assignment)

If you need to assign a full array of data to your object and wish for the variables to be passed through any custom setters there is the `setAllData` method.

```
$MyClass->setAllData([
    'foo_bar' => 'barfoo'
]);

```

Data Export
-----------

[](#data-export)

If you need to export all the data set within your object you can use 1 of 2 methods.

### toArray

[](#toarray)

```
$MyClass->toArray();

```

This will simply return all data set onto the object as an array.

### toJson

[](#tojson)

```
$MyClass->toJson();

```

This will return all the data set onto the object as a Json object. This first uses the toArray method to retrieve an array before returning the data through `json_encode`.

Changelog
---------

[](#changelog)

You can view the changelog [HERE](https://github.com/mikebarlow/GetSet/blob/master/CHANGELOG.md)

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/mikebarlow/GetSet/blob/master/CONTRIBUTING.md) for details.

License
-------

[](#license)

The MIT License (MIT). Please see [License File](https://github.com/mikebarlow/GetSet/blob/master/LICENSE) for more information.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3248d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/43972359?v=4)[snsmurf](/maintainers/snsmurf)[@snsmurf](https://github.com/snsmurf)

---

Top Contributors

[![mikebarlow](https://avatars.githubusercontent.com/u/293049?v=4)](https://github.com/mikebarlow "mikebarlow (8 commits)")

---

Tags

gettermagic-methodssettertraittraitgetterssettersObject Properties

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/snscripts-getset/health.svg)

```
[![Health](https://phpackages.com/badges/snscripts-getset/health.svg)](https://phpackages.com/packages/snscripts-getset)
```

###  Alternatives

[nette/robot-loader

🍀 Nette RobotLoader: high performance and comfortable autoloader that will search and autoload classes within your application.

89152.7M321](/packages/nette-robot-loader)[kkszymanowski/traitor

Add a trait use statement to existing PHP class

1305.2M16](/packages/kkszymanowski-traitor)[marcin-orlowski/lombok-php

Never write boilerplate code for your data class again!

3118.6k1](/packages/marcin-orlowski-lombok-php)[gomachan46/state-machine

simple state machine with annotations for PHP, inspired by AASM known as a Ruby state machine.

1893.9k](/packages/gomachan46-state-machine)[ebidtech/collection

A set of interfaces and traits to speed up the creation of collections

13119.8k6](/packages/ebidtech-collection)[fanmade/laravel-bitwise-trait

Simple trait to use bitwise operators on any class

1570.7k](/packages/fanmade-laravel-bitwise-trait)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
