PHPackages                             mekramy/oop-util - 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. mekramy/oop-util

ActiveLibrary

mekramy/oop-util
================

PHP class utility. method chaining and getter/setter implementation

v1.0.0(6y ago)0101MITPHPPHP &gt;=7.0

Since Apr 2Pushed 6y ago1 watchersCompare

[ Source](https://github.com/mekramy/oop-util)[ Packagist](https://packagist.org/packages/mekramy/oop-util)[ RSS](/packages/mekramy-oop-util/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (1)DependenciesVersions (2)Used By (1)

Getter/Setter
=============

[](#gettersetter)

#### Define Getter And Setter Methods

[](#define-getter-and-setter-methods)

**Note:** You need to add `\MEkramy\OOPUtil\MapGetterSetter` trait to your class

```
class Person
{
    # Use trait
    use \MEkramy\OOPUtil\MapGetterSetter;

    # class states
    private $fn;
    private $ln;

    # First name getter/setter
    public function setFirstName(string $firstName): void
    {
        $this->fn = $firstName;
    }
    public function getFirstName(): string
    {
        return $this->fn;
    }

    # Last name getter/setter
    public function setLastName(string $lastName): void
    {
        $this->ln = $lastName;
    }
    public function getLastName(): string
    {
        return $this->ln;
    }

    # Full name getter only
    public function getFullName(): string
    {
        return "{$this->fn} {$this->ln}";
    }
}
```

#### Use Property

[](#use-property)

**Note:** any property convert to camelCase style `getter`/`setter`. so you can call property in any style you like!

```
$person = new Person();
# All property access is valid
$person->first_name = 'John';
$person->firstName = 'John';
$person->FirstName = 'John';

$person->lastName = 'Doe';

echo $person->full_name;

# This line throw error because full_name has no setter method
$person->full_name = 'John Doe'; # throws InvalidArgumentException

# This line throw error because no getter/setter defined for notExistsProperty
$person->notExistsProperty = 'Oops!';
```

#### Handle Undefined Getter And Setters

[](#handle-undefined-getter-and-setters)

You can handle undefined `getter` and `setters` of class.

**Note:** do not use PHP magic `__get`/`__set` methods. you can use `__onGetFailed`/`__onSetFailed` methods instance

```
class Person
{
    # class body ...

    private $extra = [];

    # Handle undefined getters
    protected function __onGetFailed($name){
        return array_key_exists($name, $this->extra) ? $this->extra[$name] : null;
    }

    # Handle undefined setters
    protected function __onSetFailed($name, $value): void
    {
        # disallow set full_name
        if(!in_array($name, ['full_name', 'fullName', 'FullName'])){
            $this->extra[$name] = $value;
        }
    }
}
```

Now you can use any property for above example

```
$person = new Person();
$person->birth_date = '1991-1-1';
$person->skills = ['php', 'mysql'];
```

Class Method Chaining Call
==========================

[](#class-method-chaining-call)

#### Define Chainable Methods

[](#define-chainable-methods)

**Note:** You need to add `\MEkramy\OOPUtil\CanChained` trait to your class

**Note:** You can define excluded/included methods list to chaining call by override `__canChain`/`__cantChain` methods

**Note:** in this example both `__canChain`/`__cantChain` method do same work and you can override one of them only.

```
class MyClass{

    use \MEkramy\OOPUtil\CanChained;

    /**
     * Methods list to include in chaining call
     *
     * @return array
     */
    protected function __canChain(): array
    {
        return ['doFirst', 'doSecond', 'doThird'];
    }

    /**
     * Methods list to exclude in chaining call
     *
     * @return array
     */
    protected function __cantChain(): array
    {
        return ['getResult'];
    }

    public function doFirst(string $input){ ... }

    public function doSecond(string $input){ ... }

    public function doThird(string $input){ ... }

    public function getResult(): string{ ... }
}
```

#### Use Method In Chaining Mode

[](#use-method-in-chaining-mode)

To make chaining call of class methods you need to call `chaining` method first

```
$instance = new MyClass();
$instance->chaining()->doFirst('first')->doSecond('second')->doThird('third');

# this line throws error because getResult method not chainable
$instance->chaining()->doFirst('first')->getResult();
```

#### Get Method Return Value

[](#get-method-return-value)

```
# A: By calling normally without chaining
$instance->getResult();

# B: By calling getInstance method on chaining call
$instance->chaining()->doFirst('first')->getInstance()->getResult();
```

#### Use PHP \_\_call Method

[](#use-php-__call-method)

You can use PHP magic `__call` methods in your class and chaining call works fine!

```
class Dummy
{
    use \MEkramy\OOPUtil\CanChained;

    protected $calls = [];

    public function __call($name, $arguments)
    {
        $this->calls[] = $name;
    }

    public function __cantChain(){
        return ['print'];
    }

    public function print(){
        print_r($this->calls);
    }
}

$dummy = new Dummy();
$dummy->chaining()->A()->B()->C()->D();
$dummy->print(); // Print: ['A', 'B', 'C', 'D']
```

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

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

2234d ago

### Community

Maintainers

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

---

Tags

phputilchaininggettersetterOOP

### Embed Badge

![Health badge](/badges/mekramy-oop-util/health.svg)

```
[![Health](https://phpackages.com/badges/mekramy-oop-util/health.svg)](https://phpackages.com/packages/mekramy-oop-util)
```

###  Alternatives

[swlib/util

Swlib Toolkit

11144.6k3](/packages/swlib-util)[usmanhalalit/get-set-go

Dynamic Setter-Getter for PHP 5.4+

1813.4k1](/packages/usmanhalalit-get-set-go)[antares/accessible

PHP library that allows you to define your class' getters, setters and constructor with docblock annotations.

123.9k1](/packages/antares-accessible)

PHPackages © 2026

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