PHPackages                             revinate/php-getter-setter - 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. revinate/php-getter-setter

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

revinate/php-getter-setter
==========================

PHP library to simplify getting and setting values in arrays and objects.

1.0.0(5y ago)131.3k—7.1%3[1 issues](https://github.com/revinate/php-getter-setter/issues)2MITPHPPHP &gt;=7CI failing

Since Sep 13Pushed 5y ago47 watchersCompare

[ Source](https://github.com/revinate/php-getter-setter)[ Packagist](https://packagist.org/packages/revinate/php-getter-setter)[ RSS](/packages/revinate-php-getter-setter/feed)WikiDiscussions master Synced 1mo ago

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

phpGetterSetter
===============

[](#phpgettersetter)

PHP Library for simplifying getting and setting values in arrays or objects

Summary
-------

[](#summary)

At its core, this library is composed of 3 function pairs:

- `get` / `set` -- functions to get and set nested values in an array, object, or class given a field name using dot notation
- `getValue` / `setValue` -- functions to get and set values in an array, object, or class.
- `getValueByArrayPath` / `setValueByArrayPath` -- used by `get` and `set` to access the nested values by specifying the path in an array.

Instalation
-----------

[](#instalation)

Use Composer:

```
"require": {
    "revinate/php-getter-setter": "~0.2"
},
```

Usage
-----

[](#usage)

To make things easier, include the follow use statement at the top of your files:

```
use Revinate\GetterSetter as gs;
```

get and set
-----------

[](#get-and-set)

### Getting a Value

[](#getting-a-value)

```
$name = gs\get($data, 'name');
```

### Getting a Value with a default

[](#getting-a-value-with-a-default)

```
$name = gs\get($data, 'count', 0);
```

### Setting a Value

[](#setting-a-value)

```
$updatedData = gs\set($data, 'count', 42);
```

Here is an example unit test to give a bit more context.

```
public function testExampleJson() {
    $json = '{"name":"example","type":"json","value":22}';
    $data = json_decode($json);
    $name = gs\get($data, 'name');
    $missing = gs\get($data, 'missing');
    $this->assertEquals('example', $name);
    $this->assertNull($missing);
}
```

### Differences between objects and arrays

[](#differences-between-objects-and-arrays)

With `setValue`, an object will get updated when a field value is set. But it is different with arrays because they are immutable. Only the array returned from `setValue` will have the updated fields. Watch out for `ArrayObjects`, they will get updated just like normal `object`s.

Example Unit Test showing difference between objects and arrays:

```
public function testExampleArrayVsObject() {
    $json      = '{"name":"example","type":"json","value":22}';
    $object    = json_decode($json);
    $array     = (array)$object;
    // The object gets updated as well
    $newObject = gs\set($object, 'type', 'Object');
    // Only the new array contains the update.
    $newArray  = gs\set($array, 'type', 'Array');
    $this->assertEquals($object, $newObject);
    $this->assertNotEquals($array, $newArray);
}
```

Getting and Setting Nested Values
---------------------------------

[](#getting-and-setting-nested-values)

`getPathValue` and `setPathValue` provide an easy shortcut for getting and setting nested values.

```
{
    "first_name" : "Joe",
    "last_name" : "Rock",
    "address" : {
        "street":"1 Main St.",
        "city":"Little Rock",
        "state":"Arkansas"
    },
    "profession":"Stone cutter"
}
```

Example accessing:

```
$json = $this->getEmployeeJsonData();
$data = json_decode($json);
$this->assertEquals('Arkansas', gs\get($data, 'address.state'));
$this->assertNull(gs\get($data, 'address.zip'));
```

The notation is longer than `$data->address->state`, but it will not blow up where this will: `$data->address->zip`.

Support for Getters, Setters, Has'ers, and Is'ers.
--------------------------------------------------

[](#support-for-getters-setters-hasers-and-isers)

These functions support getters, setters and magic methods use by many ORM systems like Doctrine.

Support for Magic methods like \_\_get and \_\_set
--------------------------------------------------

[](#support-for-magic-methods-like-__get-and-__set)

Example Data Class

```
class MagicAccessTestClass {

    protected $values = array();

    function __get($name) {
        return $this->values[$name];
    }
    function __isset($name) {
        return isset($this->values[$name]);
    }
    function __set($name, $value) {
        $this->values[$name] = $value;
    }
}
```

Sample Usage:

```
public function testMagicMethods() {
    $data = new MagicAccessTestClass();
    $data->first_name = 'Joe';
    $data->last_name = 'Rock';
    gs\set($data, 'profession', 'Stone cutter');
    gs\set($data, 'address', new MagicAccessTestClass());
    gs\setPathValue($data, 'address.city', 'Little Rock');
    $this->assertEquals('Joe', gs\get($data, 'first_name'));
    $this->assertEquals('Rock', gs\get($data, 'last_name'));
    $this->assertEquals('Little Rock', gs\get($data, 'address.city'));
}
```

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity30

Limited adoption so far

Community22

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 76.5% 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

Every ~249 days

Recently: every ~436 days

Total

8

Last Release

2154d ago

Major Versions

0.2.3 → 1.0.02020-06-25

PHP version history (2 changes)0.1.0PHP &gt;=5.3.3

1.0.0PHP &gt;=7

### Community

Maintainers

![](https://www.gravatar.com/avatar/5865c6f734a730564c58798b9b54c2786dda2b7b7604c6e300f1e417653d9097?d=identicon)[revinate](/maintainers/revinate)

---

Top Contributors

[![Jason-Rev](https://avatars.githubusercontent.com/u/4850573?v=4)](https://github.com/Jason-Rev "Jason-Rev (13 commits)")[![ccarrillot](https://avatars.githubusercontent.com/u/5246308?v=4)](https://github.com/ccarrillot "ccarrillot (3 commits)")[![talon55](https://avatars.githubusercontent.com/u/649666?v=4)](https://github.com/talon55 "talon55 (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/revinate-php-getter-setter/health.svg)

```
[![Health](https://phpackages.com/badges/revinate-php-getter-setter/health.svg)](https://phpackages.com/packages/revinate-php-getter-setter)
```

###  Alternatives

[anthonybudd/wp_route

1041.9k](/packages/anthonybudd-wp-route)

PHPackages © 2026

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