PHPackages                             steevanb/composer-overload-class - 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. steevanb/composer-overload-class

ActiveLibrary

steevanb/composer-overload-class
================================

Add extra to composer.json, to overload autoloaded class

1.4.0(4y ago)25103.4k↓100%7MITPHPPHP ^5.4.0 || ^7.0 || ^8.0CI failing

Since Jul 16Pushed 4y ago1 watchersCompare

[ Source](https://github.com/steevanb/composer-overload-class)[ Packagist](https://packagist.org/packages/steevanb/composer-overload-class)[ RSS](/packages/steevanb-composer-overload-class/feed)WikiDiscussions master Synced today

READMEChangelog (8)Dependencies (1)Versions (13)Used By (0)

[![version](https://camo.githubusercontent.com/b298a54dd82af011a555d231db1cc43fbceffa63a0ae3ef1cfb25b82fd3930b0/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f76657273696f6e2d312e342e302d677265656e2e737667)](https://github.com/steevanb/composer-overload-class/tree/1.4.0)[![composer](https://camo.githubusercontent.com/c1c2d63655d793a8bfeba5e0115178f719c3c5999f6c9d82f069b374f094d0e0/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f6d706f7365722d253545312e30253743253743253545322e302d626c75652e737667)](https://getcomposer.org)[![Lines](https://camo.githubusercontent.com/cf43781939e15ed3d551a0d4f3824c3a36af3112323347b9041d330cdbeb2738/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f64652532306c696e65732d3530382d677265656e2e737667)](https://camo.githubusercontent.com/cf43781939e15ed3d551a0d4f3824c3a36af3112323347b9041d330cdbeb2738/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f64652532306c696e65732d3530382d677265656e2e737667)[![Total Downloads](https://camo.githubusercontent.com/f5b95e51ff3855a3b973c00ac65339481f5c9c4d84e2806f31aed421399038cc/68747470733a2f2f706f7365722e707567782e6f72672f7374656576616e622f636f6d706f7365722d6f7665726c6f61642d636c6173732f646f776e6c6f616473)](https://camo.githubusercontent.com/f5b95e51ff3855a3b973c00ac65339481f5c9c4d84e2806f31aed421399038cc/68747470733a2f2f706f7365722e707567782e6f72672f7374656576616e622f636f6d706f7365722d6f7665726c6f61642d636c6173732f646f776e6c6f616473)

composer-overload-class
-----------------------

[](#composer-overload-class)

Allow to overload autoloaded classes, to include your files instead of supposed ones.

Sometimes, you need to overload a class from a dependency. But you can't, cause you've found a nice "new Foo\\Bar()" somewhere in this dependency...

So, if your project use Composer autoload (like Symfony projects for example), you can use steevanb/composer-overload-class.

With a simple configuration, you can specify to Composer in which file it will find a class (your file), instead of let him find original file with class namespace. Cause you can't change namespace and class name of original class, otherwise all dependencies to this namespace / class name will fail (use Foo\\Bar, method parameter type, etc), composer-overload-class will clone Foo\\Bar class content into ComposerOverloadClass\\Foo namespace.

Your class need to have exact same namespace as overloaded one, and you can extends ComposerOverloadClass\\Foo\\Bar if you need.

[![schema](schema.png)](schema.png)

[Changelog](changelog.md)

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

[](#installation)

```
composer require steevanb/composer-overload-class ^1.4
```

Configuration
-------------

[](#configuration)

To overload a class, you need to configure it via your composer.json.

Example taken from [steevanb/doctrine-stats](https://github.com/steevanb/doctrine-stats), to overload Doctrine ObjectHydrator :

```
# composer.json
{
    "scripts": {
        "__comment": "Add a script who generate cloned classes, when autoload is generated",
        "pre-autoload-dump": "steevanb\\ComposerOverloadClass\\OverloadClass::overload"
    },
    "extra": {
        "__comment": "Path to a writable directory, where overloaded classes will be cloned, with it's namespace prefixed by ComposerOverloadClass, in prod + dev env",
        "composer-overload-cache-dir": "var/cache/prod",
        "__comment": "Path to a writable directory, in dev env",
        "composer-overload-cache-dir-dev": "var/cache/dev",
        "__comment": "Classes to overload, in prod + dev env",
        "composer-overload-class": {
            "__comment": "Fully qualified class name you want to overload",
            "Doctrine\\ORM\\Internal\\Hydration\\ObjectHydrator": {
                "__comment": "Path to original file, who contains the class you want to overload",
                "original-file": "vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php",
                "__comment": "Path to your file, who contains your class",
                "overload-file": "vendor/steevanb/doctrine-stats/ComposerOverloadClass/Doctrine/ORM/Internal/ObjectHydrator.php",
                "__comment": "false (default) : duplicate original class, add ComposerOverloadClass namespace prefix, you can extend it",
                "__comment": "true : do not duplicate original class, you need to write all code in your classe",
                "duplicate-original-file": false
            }
        },
        "__comment": "Classes to overload, in dev env",
        "composer-overload-class-dev": {}
    },
    "autoload": {
        "psr-4": {
            "ComposerOverloadClass\\": "var/cache/prod/ComposerOverloadClass"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "ComposerOverloadClass\\": "var/cache/dev/ComposerOverloadClass"
        }
    },
}
```

When configuration is finished, you need to re-generate Composer autoload :

```
composer dumpautoload

# show overrided files
composer dumpautoload -v
```

That's all folks !

Example with Doctrine ObjectHydrator
------------------------------------

[](#example-with-doctrine-objecthydrator)

Example taken from steevanb/doctrine-stats, to overload Doctrine ObjectHydrator, to add a timer when entities are hydrated :

```
# src/ComposerOverloadClass/Doctrine/ORM/Internal/ObjectHydrator.php

namespace Doctrine\ORM\Internal\Hydration;

use Doctrine\ORM\EntityManagerInterface;
use steevanb\DoctrineStats\Doctrine\ORM\Event\HydrationEventsTrait;

# extends cloned ObjectHydrator class, I just want to change hydrateAllData() code
class ObjectHydrator extends \ComposerOverloadClass\Doctrine\ORM\Internal\Hydration\ObjectHydrator
{
    use HydrationEventsTrait;

    protected function getEntityManager(): EntityManagerInterface
    {
        return $this->_em;
    }

    protected function hydrateAllData(): void
    {
        $eventId = $this->dispatchPreHydrationEvent();
        parent::hydrateAllData();
        $this->dispatchPostHydrationEvent($eventId);
    }
}
```

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity36

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity75

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

Recently: every ~400 days

Total

12

Last Release

1573d ago

PHP version history (2 changes)1.0.0PHP ^5.4.0 || ^7.0

1.4.0PHP ^5.4.0 || ^7.0 || ^8.0

### Community

Maintainers

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

---

Top Contributors

[![steevanb](https://avatars.githubusercontent.com/u/5063383?v=4)](https://github.com/steevanb "steevanb (32 commits)")

---

Tags

composerphp

### Embed Badge

![Health badge](/badges/steevanb-composer-overload-class/health.svg)

```
[![Health](https://phpackages.com/badges/steevanb-composer-overload-class/health.svg)](https://phpackages.com/packages/steevanb-composer-overload-class)
```

PHPackages © 2026

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