#[Before]
Runs before the method. It can check access, validate the arguments or replace them with setArguments().
Security checks · validation
public function check(MethodInvocation $invocation): void
Interceptez ce que les autres ne peuvent pas atteindre. Méthodes publiques, protégées, privées, finales, statiques, méthodes d’enums et de classes readonly. Le nouveau moteur va au-delà des limites de l’interception classique par proxy.
Un comportement définit ce qui s’exécute, et quand. Une règle de correspondance définit exactement où il s’applique.
Before, After, AfterThrowing et Around décident quand un comportement s’exécute : avant la méthode, après elle, sur une exception, ou à sa place. Les introductions ajoutent des interfaces.
Les voir à l’œuvreUne seule expression sélectionne le code : méthodes privées et statiques, enums, traits, classes readonly, appels de fonctions, new et vos propres attributs.
Suivre la visite guidéeSaisissez un pointcut, modifiez le PHP et voyez, ligne par ligne, quelles méthodes, quelles propriétés et quels appels il sélectionne.
Ouvrir le playgroundExécutez de la logique avant, après ou autour d’un appel de méthode. Gérez les exceptions, modifiez le flux d’exécution ou ajoutez de nouvelles capacités, le tout par des comportements déclaratifs.
Runs before the method. It can check access, validate the arguments or replace them with setArguments().
Security checks · validation
public function check(MethodInvocation $invocation): voidRuns after the method, also when it throws, like a finally block.
Audit trails · cleanup · metrics
public function audit(MethodInvocation $invocation): voidRuns only when the method throws, and receives the exception. The exception is rethrown afterwards.
Error logging · alerts
public function alert(MethodInvocation $invocation, \Throwable $e): voidRuns instead of the method and decides whether to call proceed(). Its return value becomes the result.
Caching · transactions · timing
public function cache(MethodInvocation $invocation): mixedAn introduction adds an interface, and a trait that implements it, to every class the pointcut selects. It sits on a property of the behavior class, and the classes themselves don’t change.
JsonSerializable on models · marker interfaces
#[DeclareParents('within(App\Model\*)', interfaceName: \JsonSerializable::class, traitName: JsonSerializableTrait::class)]
protected null $jsonSerializable;Ciblez méthodes, propriétés et attributs avec des règles de correspondance expressives. Voyez exactement ce que sélectionne chaque règle.
execution() selects method calls: -> for instance methods, :: for static ones.
execution(public App\Billing\Invoice->pay(*))The everyday case: one method of one class. Use * in the class or the method name to select many.
SelectsInvoice->pay() · line 61
execution(protected App\Billing\Invoice->audit(*))Internal steps that subclasses and decorators were never meant to see.
SelectsInvoice->audit() · line 79
execution(public App\Billing\Invoice::issue(*)):: selects static methods: named constructors, factories, helpers.
SelectsInvoice::issue() · line 55
execution(final public App\Billing\Invoice->total(*))Modifiers separated by spaces must all be present: final and public. The method stays final.
SelectsInvoice->total() · line 74
execution(private App\Billing\Invoice->recalculate(*))The method stays private. No subclass, no decorator, no change to the class.
SelectsInvoice->recalculate() · line 84
execution(public App\Billing\HasNumber->number(*))Attach the behavior to the trait itself, and it runs in every class that uses the trait.
SelectsHasNumber->number() · line 21
execution(public App\Billing\Status->label(*))Methods you declare on an enum. The built-in cases(), from() and tryFrom() are never intercepted.
SelectsStatus->label() · line 13
access() selects reads and writes, woven into native PHP 8.4 property hooks.
access(public App\Billing\Invoice->customerEmail)Every read and every write of the property, with no getter or setter in the class.
SelectsInvoice->customerEmail · line 45
access(protected App\Billing\Invoice->notes)Writes from inside the class go through the middleware too, here the ones made by audit().
SelectsInvoice->notes · line 49
access(private App\Billing\Invoice->total)A constructor-promoted property counts: the assignment the constructor makes is intercepted as a write.
SelectsInvoice->total (promoted) · line 53
access(private(set) App\Billing\Invoice->status)public private(set): anyone reads, only the class writes. The proxy keeps both visibilities.
SelectsInvoice->status · line 47
access(final public App\Billing\Invoice->currency)A final property declared in the class is intercepted like any other.
SelectsInvoice->currency · line 51
access(public App\Billing\Invoice->*)Readonly properties are left out, since PHP allows no hooks on them; so are static and already-hooked ones.
SelectsInvoice->customerEmail · line 45Invoice->status · line 47Invoice->currency · line 51Invoice->id (promoted): readonly property, not intercepted
Your own attributes: mark the code, and the pointcut follows the mark.
@within(App\Attribute\Monitored)Every method and property of each class marked #[Monitored], including the ones it takes from traits.
SelectsInvoice->customerEmail · line 45Invoice->status · line 47Invoice->notes · line 49Invoice->currency · line 51Invoice->total (promoted) · line 53and 8 moreInvoice->issued: static property, not interceptedInvoice->id (promoted): readonly property, not intercepted
@execution(App\Attribute\Auditable)Every method marked #[Auditable], wherever you put the attribute next.
SelectsInvoice->refund() · line 68
@access(App\Attribute\Auditable)Every property marked #[Auditable]: reads and writes.
SelectsInvoice->customerEmail · line 45
Whole classes, PHP function calls, object creation and the first load of a class.
within(App\Billing\Money)within() selects every member of a class. The methods of a readonly class are intercepted; its properties are readonly, so they are not.
SelectsMoney->__construct() · line 29Money->add() · line 31Money->cents (promoted): readonly property, not intercepted
execution(App\Billing\file_put_contents(*))Calls of a PHP function from code in your namespace, written without a leading backslash.
Selectsfile_put_contents() call · line 71
initialization(App\Billing\Money)Every new Money(…) in woven code: count, log or check the objects your code creates.
Selectsnew Money(…) · line 33new Money(…) · line 70
staticinitialization(App\Billing\*)Runs once per class, right after the class is loaded. Here: the enum and both classes, but not the trait.
Selectsenum Status loads · line 8class Money loads · line 27class Invoice loads · line 38
<?php
namespace App\Billing;
use App\Attribute\Auditable;
use App\Attribute\Monitored;
enum Status: string
class load
{
case Draft = 'draft';
case Paid = 'paid';
public function label(): string
method
{
return ucfirst($this->value);
}
}
trait HasNumber
{
public function number(): string
method
{
return sprintf('INV-%05d', $this->id);
}
}
final readonly class Money
class load
{
public function __construct(public int $cents) {}
methodreadonly property: not intercepted
public function add(Money $other): Money
method
{
return new Money($this->cents + $other->cents);
new
}
}
#[Monitored]
class Invoice
class load
{
use HasNumber;
method number() from HasNumber
public static int $issued = 0;
static property: not intercepted
#[Auditable]
public string $customerEmail = '';
propertypropertypropertyproperty
public private(set) Status $status = Status::Draft;
propertypropertyproperty
protected string $notes = '';
propertyproperty
final public string $currency = 'EUR';
propertypropertyproperty
public function __construct(public readonly int $id, private Money $total) {}
propertyreadonly property: not interceptedpropertymethodreadonly property: not intercepted
public static function issue(int $id, Money $total): static
static methodstatic method
{
self::$issued++;
return new static($id, $total);
}
public function pay(): void
methodmethod
{
$this->status = Status::Paid;
$this->audit('paid');
}
#[Auditable]
public function refund(int $cents, string $reason): void
methodmethod
{
$this->total = $this->recalculate(new Money(-$cents));
new
file_put_contents('refunds.log', "{$this->id}: {$reason}\n", FILE_APPEND);
function call
}
final public function total(): Money
methodmethod
{
return $this->total;
}
protected function audit(string $event): void
protected methodprotected method
{
$this->notes .= $event . "\n";
}
private function recalculate(Money $change): Money
private methodprivate method
{
return $this->total->add($change);
}
}
Saisissez un pointcut, modifiez le PHP et voyez, ligne par ligne, quelles méthodes, quelles propriétés et quels appels il sélectionne.
Essayer votre pointcut