Home >Backend Development >PHP Tutorial >Under the Hood of Yii's Component Architecture, Part 2
This article continues our exploration of Yii Framework's CComponent
class, focusing on event-driven programming in PHP. This is part 2 of a three-part series demonstrating how Yii leverages a component-based architecture to manage properties, configuration, events, and behaviors. Part 1 covered property implementation using PHP's magic methods; here, we delve into event handling.
Key Concepts:
CComponent
class underpins its component-based architecture, providing a robust mechanism for handling events.CComponent
, defining, triggering, and attaching event handlers. This involves defining event methods (prefixed with "on"), triggering them with $this->raiseEvent()
, and attaching handlers using onEventName = callback
.Implementing Events:
Event implementation involves three key steps: defining, attaching handlers, and triggering the event.
1. Defining Events:
Events are defined by creating methods prefixed with "on". For instance, a "user registration" event might be defined as onUserRegistered
. This method resides within the relevant module (e.g., user module).
<?php public function onUserRegistered($event) { $this->raiseEvent("onUserRegistered", $event); }
The event is added to a component (e.g., Yii::app()->user
) for application-wide accessibility.
2. Triggering Events:
The event is triggered within the appropriate controller (e.g., user registration controller).
<?php public function actionUserRegister() { // ... user registration logic ... $e = new CEvent($this, array("user" => $user)); Yii::app()->user->onUserRegistered($e); }
The CEvent
object holds the event source and any relevant data. raiseEvent()
then executes all attached handlers.
3. Attaching Event Handlers:
Event handlers are attached using the onEventName = callback
syntax. For example:
<?php public function init() { Yii::app()->user->onUserRegistered = array($this, "sendMyEmail"); } public function sendMyEmail($event) { $user = $event->params["user"]; mail($user->email, "Welcome!", "Hello..."); }
This attaches the sendMyEmail
method as a handler for onUserRegistered
. Anonymous functions (PHP 5.3 ) are also supported.
Yii's CComponent
Implementation:
Yii's clever implementation within CComponent
manages event definition, triggering, and attachment.
__set
magic method handles attaching callbacks to events, storing them in the $_e
private member variable. This variable is an array where keys are event names and values are arrays of callbacks.raiseEvent
method iterates through the callbacks associated with an event and executes them.<?php public function onUserRegistered($event) { $this->raiseEvent("onUserRegistered", $event); }
Conclusion:
Events provide a powerful mechanism for creating robust, flexible, and reusable code. This article illustrated how Yii's CComponent
class implements this pattern in PHP, a concept applicable across various frameworks and languages. The final part of this series will cover behaviors, another method for extending component functionality.
(Frequently Asked Questions section omitted for brevity, as it's largely redundant with the content already provided.)
The above is the detailed content of Under the Hood of Yii's Component Architecture, Part 2. For more information, please follow other related articles on the PHP Chinese website!