PHPackages                             christophrumpel/method-overrider - 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. christophrumpel/method-overrider

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

christophrumpel/method-overrider
================================

A PHP package to override methods.

v1.4.0(7mo ago)345MITPHPPHP ^8.3.0CI passing

Since Oct 29Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/christophrumpel/method-overrider)[ Packagist](https://packagist.org/packages/christophrumpel/method-overrider)[ RSS](/packages/christophrumpel-method-overrider/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (6)Versions (7)Used By (0)

 [![GitHub Workflow Status (master)](https://github.com/christophrumpel/method-overrider.git/actions/workflows/tests.yml/badge.svg)](https://github.com/christophrumpel/method-overrider.git/actions) [![Total Downloads](https://camo.githubusercontent.com/903d6e945d8bbb08e6ca879f9de15b6d8e3af9a367582e8bc2f938ef567da4f0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6368726973746f706872756d70656c2f6d6574686f642d6f7665727269646572)](https://packagist.org/packages/christophrumpel/method-overrider) [![Latest Version](https://camo.githubusercontent.com/ec8ab42e13d414aefcda29e11684c9bdb9e56944ff48eb585d44b619aece29b2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6368726973746f706872756d70656c2f6d6574686f642d6f7665727269646572)](https://packagist.org/packages/christophrumpel/method-overrider) [![License](https://camo.githubusercontent.com/0444dc7499f6c7880383b5bfa00423972dc35e7380d8d35a9a9477ce60ba417a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6368726973746f706872756d70656c2f6d6574686f642d6f7665727269646572)](https://packagist.org/packages/christophrumpel/method-overrider)

PHP Method Overrider
====================

[](#php-method-overrider)

> ⚠️ **Experimental Package** - This is a work in progress and not intended for production use.

Dynamically override specific methods of any PHP class at runtime while preserving method signatures and providing access to the original implementation.

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

[](#installation)

```
composer require christophrumpel/method-overrider
```

Usage
-----

[](#usage)

```
use ChristophRumpel\MethodOverrider\MethodOverrider;

class MathService
{
    public function add(int $a, int $b): int
    {
        return $a + $b;
    }
}

$overrider = new MethodOverrider();

// Override the add method to multiply instead
$instance = $overrider->override(
    class: MathService::class,
    methodNames: 'add',
    implementations: function (callable $original, int $a, int $b): int {
        // You can still call the original method
        $originalResult = $original();

        // Or implement completely new logic
        return $a * $b;
    }
);

echo $instance->add(2, 3); // Output: 6 (multiplied instead of added)
```

How it works
------------

[](#how-it-works)

The package uses reflection to analyze method signatures and dynamically generates new classes that extend your original class. The overridden methods wrap your custom implementations while providing access to the original method via a closure.

**Note:** The `override()` method uses `eval()` to dynamically create and instantiate the overridden class. If you prefer to avoid `eval()`, see the alternative method below.

Alternative: Generate Class Without `eval()`
--------------------------------------------

[](#alternative-generate-class-without-eval)

If you want to avoid `eval()`, you can use the `generateOverriddenClass()` method which returns the generated class code as a string along with metadata:

```
$result = $overrider->generateOverriddenClass(
    class: MathService::class,
    methodNames: 'add',
    implementations: function (callable $original, int $a, int $b): int {
        return $a * $b;
    }
);

// Returns an array with:
// - 'content': The generated PHP class code as a string
// - 'implementations': The provided implementations array
// - 'className': The generated class name

// You can then save this to a file or handle it however you prefer
file_put_contents('path/to/MathServiceCacheProxy.php', $result['content']);
```

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

[](#requirements)

- PHP 8.3+

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance63

Regular maintenance activity

Popularity11

Limited adoption so far

Community7

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

Every ~84 days

Total

5

Last Release

223d ago

### Community

Maintainers

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

---

Top Contributors

[![christophrumpel](https://avatars.githubusercontent.com/u/1394539?v=4)](https://github.com/christophrumpel "christophrumpel (19 commits)")

---

Tags

phpmethodsoverriding

###  Code Quality

TestsPest

Static AnalysisPHPStan, Rector

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/christophrumpel-method-overrider/health.svg)

```
[![Health](https://phpackages.com/badges/christophrumpel-method-overrider/health.svg)](https://phpackages.com/packages/christophrumpel-method-overrider)
```

###  Alternatives

[imanghafoori/laravel-anypass

A minimal yet powerful package to help you in development.

21421.6k](/packages/imanghafoori-laravel-anypass)

PHPackages © 2026

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