craftcms / docs

Documentation for Craft CMS, Craft Commerce, and other official products.
https://craftcms.com/docs
38 stars 144 forks source link

[Content]: Manually attaching behaviors #602

Open AugustMiller opened 2 months ago

AugustMiller commented 2 months ago

Product

Craft CMS

Description

In a conversation about modifying built-in services, @bencroker noted that we don't mention yii\base\Component::attachBehavior() as an alternative to the event-based approach.

Most classes in Craft ultimately inherit from this (including our core services), so it's possible to augment them without going via an intermediate event listener:

Craft::$app->getGc()->attachBehavior('myPlugin.garbageEnhancements', TurboTrash::class);

This bears some similarities to our discussion of instance-level events.