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
Introduce interfaces and traits to existing classes without modifying their source code or building inheritance hierarchies.
Add an interface and its implementation to every class a pointcut selects, from one place.
Give the DTOs or models of a third-party package your application’s interface and a reusable trait, without forking it.
See the exampleInterception changes what happens when code runs. An introduction changes what a class can do: its interfaces and methods.
Compare themHow introductions are declared, and how they combine with the other behaviors of the same class.
Read the docsBoth are declared in a behavior class (an aspect, in the API), and both leave your source files alone.
#[Around('@execution(App\Attribute\Cacheable)')]The method keeps its signature. A behavior runs around every call: caching, logging, transactions.
Intercept anything#[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 exampleYour 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.
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.
AppKernel::getInstance()->init([ // … 'includePaths' => [ __DIR__ . '/src', __DIR__ . '/vendor/acme/payments-sdk/src', ],]);$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']
<?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,
) {}
}
<?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;
}
<?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
}
More in docs/advices.md and docs/configuration.md.
The package stays as Composer installed it. Updates keep arriving with composer update, and the next cache warm-up weaves them again.
The class keeps its name, its modifiers and its parents. The interface and the trait are added to it, not to a subclass.
instanceof and type declarations work, because the class PHP loads really implements it.