PHPackages                             macfja/composer-class-rewrite - 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. macfja/composer-class-rewrite

ActiveComposer-plugin[Utility &amp; Helpers](/categories/utility)

macfja/composer-class-rewrite
=============================

Composer plugin to handle class rewrite

v0.1.0(10y ago)239MITPHP

Since Jan 26Pushed 10y ago1 watchersCompare

[ Source](https://github.com/MacFJA/ComposerClassRewrite)[ Packagist](https://packagist.org/packages/macfja/composer-class-rewrite)[ RSS](/packages/macfja-composer-class-rewrite/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (1)Dependencies (5)Versions (2)Used By (0)

Composer Class-Rewrite
======================

[](#composer-class-rewrite)

[![Latest Version](https://camo.githubusercontent.com/5be03395adf3528ec4fa9fb6c9ae37da537c88f80b484cb1adead61a7d28a4aa/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f4d6163464a412f436f6d706f736572436c617373526577726974652e737667)](https://github.com/MacFJA/ComposerClassRewrite/releases)[![Software License](https://camo.githubusercontent.com/13a981219a0dc5c8811400487b0d2e210da717f3e85c6d0ab6c03325583b7012/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6d6163666a612f636f6d706f7365722d636c6173732d726577726974652e737667)](LICENSE.md)[![Total Downloads](https://camo.githubusercontent.com/266aeaf30b5f43d172bcfaee9cd46b39627fe3603f40444849cb3377f13e5f4f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d6163666a612f636f6d706f7365722d636c6173732d726577726974652e737667)](https://packagist.org/packages/macfja/composer-class-rewrite)

What is Composer Class-Rewrite
------------------------------

[](#what-is-composer-class-rewrite)

Composer Class-Rewrite is a [Composer](https://getcomposer.org) plugin that allow you to rewrite almost\[1\][1](#user-content-fn-1-356a964a8e2596b9972d7dff363becda) any classes of your project.

Principle\[2\][2](#user-content-fn-2-356a964a8e2596b9972d7dff363becda)
----------------------------------------------------------------------

[](#principle22)

The idea is to scan every classes of the project to find classes declared as a rewrite. Then we make some modification on the parent (rewritten) class (*a copy of the parent class*) and the rewriter class (*a copy of the rewriter class*) and finally add them to the Composer autoload (before **PSR-0** and **PSR-4** classes). So, when a class ask for the rewritten class, Composer will return our modified rewiter class.

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

[](#installation)

With [Composer](https://getcomposer.org):

```
$ composer require macfja/composer-class-rewrite
```

Usage
-----

[](#usage)

```
# File:  example/A.php
namespace Example;
class A
{
    public function who()
    {
    	return 'A';
    }
}
```

```
# File:  example/B.php
namespace Example;
class B extends A
{
    public function who()
    {
    	return parent::who() . '-B';
    }
}
```

```
# File:  example/C.php
namespace Example;
class C extends A implements \MacFJA\ClassRewrite\Rewriter
{
    public function who()
    {
    	return parent::who() . '-C';
    }
}
```

```
$ composer dump-autoload # Mandatory after any changes in Example\A or in Example\C
```

```
$b = new B();
echo $b->who(); // Output: "A-C-B"
```

Limitations
--------------------------------------------------

[](#limitations)

- Only work for **PSR-0** and **PSR-4** namespace.
- Only work if Composer is the first autoloader.
- Only work on non-dev dependency.
- Only work on class (not on trait, nor on interface).
- The rewriter class **MUST** have the same namespace than the rewritten class.
- You have to rebuild the autoload for every rewriter/rewritten changes.
- Have some side effects *(see example below)*
    - Class context is changed (magic constants).
    - Multiple call if you instanciate the rewriter class.

### Side effects

[](#side-effects)

#### Multiple call

[](#multiple-call)

```
# File:  example/A.php
namespace Example;
class A
{
    public function who()
    {
    	return 'A';
    }
}
```

```
# File:  example/B.php
namespace Example;
class B extends A implements \MacFJA\ClassRewrite\Rewriter
{
    public function who()
    {
    	return parent::who() . '-B';
    }
}
```

```
# File:  example/C.php
namespace Example;
class C extends B
{
    public function who()
    {
    	return parent::who() . '-C';
    }
}
```

```
# File: test.php
require 'vendor/autoload.php';
$class = new A();
echo $class->who(); //output A-B

$class2 = new B();
echo $class2->who(); // output A-B-B
// -> the output is (A='A-B', parent of B) + '-B' (from B)

$class3 = new C();
echo $class3->who(); // output A-B-B-C
// -> the output is (A='A-B', parent of B) + '-B' (from B, parent of C) + '-C' (from C)
```

#### Magic constants

[](#magic-constants)

```
# File:  example/A.php
