PHPackages                             mrself/options - 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. mrself/options

ActiveLibrary

mrself/options
==============

v2.19.7(5y ago)0207↓50%[2 issues](https://github.com/mrself/php-options/issues)[1 PRs](https://github.com/mrself/php-options/pulls)7PHP

Since Jan 29Pushed 1y ago1 watchersCompare

[ Source](https://github.com/mrself/php-options)[ Packagist](https://packagist.org/packages/mrself/options)[ RSS](/packages/mrself-options/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (8)Versions (62)Used By (7)

Demo
----

[](#demo)

Add the trait to your class

```
class ClassWithOptions
{
    use \Mrself\Options\WithOptionsTrait;
}
```

Add the first option:

```
use Mrself\Options\Annotation\Option;

class ClassWithOptions
{
    use \Mrself\Options\WithOptionsTrait;

    /**
    * The required option to initialize the class
     * @Option()
     * @var array
     */
    private $arrayOption;

    public function getOption()
    {
        return $this->arrayOption;
    }
}
```

Initialize the class:

```
$instance = ClassWithOptions::make(['arrayOption' => ['key' => 'value']]);

// True
$instance->getOption()['key'] === 'value';
```

An exception is thrown if the option is of missed:

```
// Exception \Symfony\Component\OptionsResolver\Exception\MissingOptionsException
$instance = ClassWithOptions::make();
```

More examples
-------------

[](#more-examples)

Type resolving:

```
use Mrself\Options\Annotation\Option;

class ClassWithOptions
{
    use \Mrself\Options\WithOptionsTrait;

    /**
     * @Option()
     * @var array
     */
    private $arrayOption;

    public function getOption()
    {
        return $this->arrayOption;
    }
}

$notArray = 1;
// Exception
ClassWithOptions::make(['arrayOption' => $notArray]);
```

```
use Mrself\Options\Annotation\Option;

class ClassWithOptions
{
    use \Mrself\Options\WithOptionsTrait;

    /**
     * @Option()
     * @var \DateTime
     */
    private $arrayOption;

    public function getOption()
    {
        return $this->arrayOption;
    }
}

$notDate = 1;
// Exception
ClassWithOptions::make(['arrayOption' => $notDate]);
```

Using with container (see mrself/container)
-------------------------------------------

[](#using-with-container-see-mrselfcontainer)

```
use Mrself\Options\Annotation\Option;
use Mrself\Container\Container;
use Mrself\Container\Registry\ContainerRegistry;

$service = new \stdClass();
$service->property = 'myProperty';

$container = Container::make();
$container->set('service', $service);
ContainerRegistry::add('App', $container);

class ClassWithOptions
{
    use \Mrself\Options\WithOptionsTrait;

    /**
     * @Option()
     * @var \stdClass
     */
    private $service;

    public function getService()
    {
        return $this->service;
    }
}

$instance = ClassWithOptions::make();

// True
$instance->getService()->property === 'myProperty';
```

This trait can be used with Symfony or another framework with public services.

Suspend all errors:

```
$object->init(['.silent' => true]);
```

---

If an annotated property has a non-primitive type, the property can be resolved only of that type:

```
$object = new class {
    /**
     * @Option
     * @var \Reflection
     */
    public $option1;
};

// Throws since 'option1' expected a value of type '\Reflection'
$object->init(['option1' => 1]);
```

---

Primitive types are not processed so they should be declared in array schema:

```
new class {
    protected function getOptionsSchema()
    {
        return [
            'allowedTypes' => ['option1' => \Reflection::class]
        ];
    }
 };
```

---

Array schema has a higher priority than an annotation schema

---

An option can be set as optional:

```
$object = new class {
    /**
     * @Option(required=false)
     * @var \Reflection
     */
    public $option1;
};
```

---

Options can be preset by a specific key:

```
$object = new class {

    /**
     * @Option()
     * @var string
     */
    public $option1;
};
$object::presetOptions('nameOfPreset', [
    'option1' => 'value1'
]);
$object->init(['presetName' => 'nameOfPreset']);
$object->option1 === 'value1';
```

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance30

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity74

Established project with proven stability

 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 ~13 days

Recently: every ~24 days

Total

59

Last Release

1896d ago

Major Versions

v1.4.3 → v2.0.02019-02-02

### Community

Maintainers

![](https://www.gravatar.com/avatar/a2e0cc0bcdda42623a871a571b48037a42477cda5e2505fa56ec1d99fd4e45f5?d=identicon)[mrself](/maintainers/mrself)

---

Top Contributors

[![mrself](https://avatars.githubusercontent.com/u/9679597?v=4)](https://github.com/mrself "mrself (137 commits)")

---

Tags

dependency-injectionoptionspackagephp

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mrself-options/health.svg)

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

###  Alternatives

[phpbench/phpbench

PHP Benchmarking Framework

2.0k13.0M627](/packages/phpbench-phpbench)[symfony/form

Allows to easily create, process and reuse HTML forms

2.8k152.1M2.8k](/packages/symfony-form)[sentry/sentry

PHP SDK for Sentry (http://sentry.io)

1.9k227.1M271](/packages/sentry-sentry)[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.4k5.6M648](/packages/sylius-sylius)[nelmio/api-doc-bundle

Generates documentation for your REST API from attributes

2.3k63.6M232](/packages/nelmio-api-doc-bundle)[symfony/rate-limiter

Provides a Token Bucket implementation to rate limit input and output in your application

26847.2M146](/packages/symfony-rate-limiter)

PHPackages © 2026

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