PHPackages                             macfja/value-provider - 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. [Database &amp; ORM](/categories/database)
4. /
5. macfja/value-provider

ActiveLibrary[Database &amp; ORM](/categories/database)

macfja/value-provider
=====================

Get/Set object value with its getter/setter/property/metadata

v0.3.1(10y ago)2471MITPHP

Since Feb 14Pushed 10y ago1 watchersCompare

[ Source](https://github.com/MacFJA/value-provider)[ Packagist](https://packagist.org/packages/macfja/value-provider)[ RSS](/packages/macfja-value-provider/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (3)Used By (1)

ValueProvider
=============

[](#valueprovider)

**ValueProvider** allow you to access to your object's property value without knowing the way the object have defined to do so. Property, getter/setter? Let's `GuessProvider` take care of this.

Features
--------

[](#features)

Can access to object's properties values:

- With direct property access (like `$myObject->myProperty`)
    - Works with magic `__get` and `__set` (see below for examples and restriction)
    - Works with `protected` and `private` properties (see below for examples and restriction)
- With mutator (getter/setter) (like `$myObject->getMyProperty()` and `$myObject->setMyProperty('new value')`)
    - Works with magic `__call` (see below for examples and restriction)
- With **doctrine** **`Metadata`** (see below for examples)

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

[](#installation)

### Composer

[](#composer)

```
composer require macfja/value-provider

```

Examples
--------

[](#examples)

### Property Access

[](#property-access)

Class Person

```
class Person {
    public $firstName = '';
    public $lastName = '';
}
```

Somewhere in your code

```
$jdoe = new Person();
$provider = new PropertyProvider();

// ...

$provider->setValue($jdoe, 'firstName', 'John');
$provider->setValue($jdoe, 'lastName', 'Doe');

// ...

echo 'Hello ' . $provider->getValue($jdoe, 'firstName') . ' ' . $provider->getValue($jdoe, 'lastName');
//Output : "Hello John Doe"
```

#### Notice

[](#notice)

The properties have to be public to be accessed. (For protected and private properties, see ReflectorProvider)

### Magic `__get` and `__set` properties access

[](#magic-__get-and-__set-properties-access)

Class Person

```
class Person {
    private $_firstName = 'John';
    private $_lastName = 'Doe';

    function __get($name) {
        if (in_array($name, array('firstName', 'lastName')) {
            $propertyName = '_' . $name;
            return $this->$propertyName;
        }

        throw new \BadFunctionCallException;
    }

    function __set($name, $value) {
        if (in_array($name, array('firstName', 'lastName')) {
            $propertyName = '_' . $name;
            $this->$propertyName = $value;
            return;
        }

        throw new \BadFunctionCallException;
    }

    function __isset($name) {
        return in_array($name, array('firstName', 'lastName');
    }
}
```

Somewhere in your code

```
$jdoe = new Person();
$provider = new PropertyProvider();

// ...

$provider->setValue($jdoe, 'firstName', 'John');
$provider->setValue($jdoe, 'lastName', 'Doe');

// ...

echo 'Hello ' . $provider->getValue($jdoe, 'firstName') . ' ' . $provider->getValue($jdoe, 'lastName');
//Output : "Hello John Doe"
```

#### Notice

[](#notice-1)

The class rely on the `__isset` function to know if the property can be used with `__get` and `__set`

### Mutator Access (getter/setter)

[](#mutator-access-gettersetter)

Class Person

```
class Person {
    private $_firstName = '';
    private $_lastName = '';
    private $_known = true;

    public function getFirstName() {
        return $this->_firstName;
    }
    public function setFirstName($value) {
        $this->_firstName = $value;
    }
    public function getLastName() {
        return $this->_lastName;
    }
    public function setLastName($value) {
        $this->_firstName = $value;
    }
    public function isKnown() {
        return $this->_known;
    }
    public function setKnown($flag) {
        $this->_known = $flag;
    }
}
```

Somewhere in your code

```
$jdoe = new Person();
$provider = new MutatorProvider();

// ...

$provider->setValue($jdoe, 'firstName', 'John');
$provider->setValue($jdoe, 'lastName', 'Doe');
$provider->setValue($jdoe, 'known', false);

// ...

echo 'Hello ' . $provider->getValue($jdoe, 'firstName') . ' ' . $provider->getValue($jdoe, 'lastName');
echo ', you are ' . ($provider->getValue($jdoe, 'known') ? '' : 'un') . 'known';
//Output : "Hello John Doe, you are unknown"
```

#### Notice

[](#notice-2)

The mutator have to be public to be accessed. Getter are search in this order

- getMyProperty
- isMyProperty

### Magic `__call` mutator access

[](#magic-__call-mutator-access)

Class Person

```
class Person {
    private $_firstName = 'John';
    private $_lastName = 'Doe';
    private $_known = true;

    function __call($name, $arguments) {
        switch ($name) {
            case 'getFirstName':
                return $this->_firstName;
            case 'getLastName':
                return $this->_lastName;
            case 'isKnown':
                return $this->_known;
            case 'setFirstName':
                $this->_firstName = $argument[0];
                return;
            case 'setLastName':
                $this->_lastName = $argument[0];
                return;
            case 'setKnown':
                $this->_known = $argument[0];
                return;
        }

        throw new \BadFunctionCallException;
    }
}
```

Somewhere in your code

```
$jdoe = new Person();
$provider = new MutatorProvider();

// ...

$provider->setValue($jdoe, 'firstName', 'John');
$provider->setValue($jdoe, 'lastName', 'Doe');
$provider->setValue($jdoe, 'known', false);

// ...

echo 'Hello ' . $provider->getValue($jdoe, 'firstName') . ' ' . $provider->getValue($jdoe, 'lastName');
echo ', you are ' . ($provider->getValue($jdoe, 'known') ? '' : 'un') . 'known';
//Output : "Hello John Doe, you are unknown"
```

#### Notice

[](#notice-3)

The class rely on the implementation of the `__call` function: the function MUST throw either `\BadFunctionCallException` or `\InvalidArgumentException` if the getter/setter doesn't exist

### Doctrine Metadata

[](#doctrine-metadata)

Somewhere in your code

```
/** @type EntityManager $entityManager */
$class = 'MyClass';
$id = 1234;

$provider = new MetadataProvider();
MetadataProvider::setEntityManager($entityManager);

jdoe = $entityManager->find($class, $id);

// ...

$provider->setValue($jdoe, 'firstName', 'John');
$provider->setValue($jdoe, 'lastName', 'Doe');
$provider->setValue($jdoe, 'known', false);

// ...

echo 'Hello ' . $provider->getValue($jdoe, 'firstName') . ' ' . $provider->getValue($jdoe, 'lastName');
echo ', you are ' . ($provider->getValue($jdoe, 'known') ? '' : 'un') . 'known';
//Output : "Hello John Doe, you are unknown"
```

#### Notice

[](#notice-4)

You have to set the Doctrine `EntityManager` to the `MetadataProvider`

### ReflectorProvider

[](#reflectorprovider)

Class Person

```
class Person {
    public $firstName = 'John';
    protected $lastName = 'Doe';
    private $known = true;
}
```

Somewhere in your code

```
$jdoe = new Person();
$provider = new ReflectorProvider();

// ...

$provider->setValue($jdoe, 'firstName', 'John');
$provider->setValue($jdoe, 'lastName', 'Doe');
$provider->setValue($jdoe, 'known', false);

// ...

echo 'Hello ' . $provider->getValue($jdoe, 'firstName') . ' ' . $provider->getValue($jdoe, 'lastName');
echo ', you are ' . ($provider->getValue($jdoe, 'known') ? '' : 'un') . 'known';
//Output : "Hello John Doe, you are unknown"
```

#### Notice

[](#notice-5)

The class can access to private/protected property if you are running PHP 5.3 or newer

### GuessProvider

[](#guessprovider)

This class try to access to the value with mutator or property or reflection. It first try the mutator, if not success then try the property, and finish by trying with reflector.

### ChainProvider

[](#chainprovider)

This class allow you to try through multiple Provider. For instance, the `GuessProvider` is based on the `ChainProvider`.

Information
-----------

[](#information)

This library respect PSR-1, PSR-2, PSR-4. It have PHPUnit tests for each provider.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity54

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

Every ~4 days

Total

2

Last Release

3739d ago

### Community

Maintainers

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

---

Top Contributors

[![MacFJA](https://avatars.githubusercontent.com/u/1475671?v=4)](https://github.com/MacFJA "MacFJA (7 commits)")

---

Tags

doctrinegettersmutatorspropertiessettersdoctrinemetadataprovideraccesspropertymutatorgettersettervalue

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/macfja-value-provider/health.svg)

```
[![Health](https://phpackages.com/badges/macfja-value-provider/health.svg)](https://phpackages.com/packages/macfja-value-provider)
```

###  Alternatives

[symfony/property-info

Extracts information about PHP class' properties using metadata of popular sources

2.2k256.7M854](/packages/symfony-property-info)[thomas-schulz/doctrine-phpstorm-meta

PhpStorm meta data for expected arguments completion in Doctrine projects.

3435.0k](/packages/thomas-schulz-doctrine-phpstorm-meta)[event4u/data-helpers

Framework-agnostic PHP library for data mapping, DTOs and utilities. Includes DataMapper, SimpleDto/LiteDto, DataAccessor/Mutator/Filter and helper classes (MathHelper, EnvHelper, etc.). Works with Laravel, Symfony/Doctrine or standalone PHP.

1421.5k](/packages/event4u-data-helpers)

PHPackages © 2026

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