Violet light wraps a row of clear glass objects, giving each a new facet.
Feature · Class extensions

Add capabilities to classes you don't own.

Introduce interfaces and traits to existing classes without modifying their source code or building inheritance hierarchies.

Two ways to extend a class

Both are declared in a behavior class (an aspect, in the API), and both leave your source files alone.

Interception

Changes what happens when existing code executes.

#[Around('@execution(App\Attribute\Cacheable)')]

The method keeps its signature. A behavior runs around every call: caching, logging, transactions.

Intercept anything
Introduction

Changes what capabilities existing classes expose.

#[DeclareParents('within(Acme\Payments\Dto\*)', …)]

The class gains an interface and the methods of a trait. Nothing runs around existing calls; your code calls the new methods.

See the example
Third-party classes

Extend vendor classes without forking them.

Your export pipeline accepts anything Exportable. The payment SDK’s DTOs aren’t, and they are final. Instead of forking the package or wrapping every DTO in an adapter, one behavior class adds your interface and a reusable trait to all of them.

src/Contract/Exportable.phpyour code
interface Exportable{    /** @return array<string, scalar|null> */    public function toExportRow(): array;} // src/Export/ExportsPublicProperties.phptrait ExportsPublicProperties{    public function toExportRow(): array    {        return get_object_vars($this);    }}

Name the package in includePaths, so it is woven like your own code. Weaving vendor code costs time at warm-up, so list only the packages you extend.

public/index.phpkernel
AppKernel::getInstance()->init([    // …    'includePaths' => [        __DIR__ . '/src',        __DIR__ . '/vendor/acme/payments-sdk/src',    ],]);
Anywhere in your appusage
$charge = new Charge('ch_1', 4200, 'EUR');  // a vendor DTO $charge instanceof Exportable;  // true$exporter->write($charge);       // accepts any Exportable$charge->toExportRow();          // ['id' => 'ch_1', 'amount' => 4200, 'currency' => 'EUR']
Charge.phpvendor, untouched

        
        
        <?php
        
        
        
      
        
        
         
        
        
        
      
        
        
        declare(strict_types=1);
        
        
        
      
        
        
         
        
        
        
      
        
        
        namespace Acme\Payments\Dto;
        
        
        
      
        
        
         
        
        
        
      
        
        
        final readonly class Charge
        final and readonly
        
        
      
        
        
        {
        
        
        
      
        
        
            public function __construct(
        
        
        
      
        
        
                public string $id,
        
        
        
      
        
        
                public int $amount,
        
        
        
      
        
        
                public string $currency,
        
        
        
      
        
        
            ) {}
        
        
        
      
        
        
        }
        
        
        
      
ExportAspect.phpyour behavior class

        
        
        <?php
        
        
        
      
        
        
         
        
        
        
      
        
        
        declare(strict_types=1);
        
        
        
      
        
        
         
        
        
        
      
        
        
        namespace App\Aspect;
        
        
        
      
        
        
         
        
        
        
      
        
        
        use App\Contract\Exportable;
        
        
        
      
        
        
        use App\Export\ExportsPublicProperties;
        
        
        
      
        
        
        use Go\Aop\Aspect;
        
        
        
      
        
        
        use Go\Lang\Attribute\DeclareParents;
        
        
        
      
        
        
         
        
        
        
      
        
        
        final class ExportAspect implements Aspect
        
        
        
      
        
        
        {
        
        
        
      
        
        
            #[DeclareParents('within(Acme\Payments\Dto\*)', interfaceName: Exportable::class, traitName: ExportsPublicProperties::class)]
        every DTO of the package
        
        
      
        
        
            protected null $exportable;
        
        
        
      
        
        
        }
        
        
        
      
Charge.phpgenerated

        
        
        <?php
        
        
        
      
        
        
        declare(strict_types=1);
        
        
        
      
        
        
        namespace Acme\Payments\Dto;
        
        
        
      
        
        
         
        
        
        
      
        
        
        use Go\Aop\Framework\InterceptorInjector;
        
        
        
      
        
        
        use Go\Aop\Framework\Interceptor;
        
        
        
      
        
        
        use Go\Aop\Framework\The;
        
        
        
      
        
        
        final readonly class Charge implements \App\Contract\Exportable, \Go\Aop\Proxy
        your interface
        
        
      
        
        
        {
        
        
        
      
        
        
            use ChargeOriginalTrait, \App\Export\ExportsPublicProperties;
        your trait
        
        
      
        
        
        }
        
        
        
      

Behavior without inheritance

More in docs/advices.md and docs/configuration.md.

  • No fork to maintain

    The package stays as Composer installed it. Updates keep arriving with composer update, and the next cache warm-up weaves them again.

  • Final and readonly classes too

    The class keeps its name, its modifiers and its parents. The interface and the trait are added to it, not to a subclass.

  • A real interface

    instanceof and type declarations work, because the class PHP loads really implements it.