Overview
The Event system in S-PHP provides a simple way to implement event-driven architecture in your application. You can register listeners for specific events and dispatch those events when needed.Basic Usage
Registering Event Listeners
Use theEvent::handle() method to register a callback for an event:
Dispatching Events
Trigger an event usingEvent::dispatch():
user.registered event.
Event Priorities
Setting Priority
Listeners can have priority levels (higher numbers execute first):Default Priority
If no priority is specified, the default is1:
Event with Jobs
You can dispatch events with job instances:- The job is executed automatically
- All registered callbacks are still invoked
- The job’s task name is displayed in output
Multiple Listeners
Register multiple callbacks for the same event:Event Naming Conventions
Use descriptive, dot-separated event names:Common Event Patterns
User Events
Application Events
Data Events
Event Class API
handle()
Register an event listener:$event- Event name (string)$callback- Function to execute (callable)$priority- Execution priority (int, default: 1)
dispatch()
Trigger an event:$event- Event name (string)$job- Optional Job instance to execute
Debug Output
The Event system outputs debug information:Example: Complete Event System
Best Practices
- Descriptive Names - Use clear, dot-separated event names
- Priority Levels - Use priorities to control execution order
- Decouple Logic - Keep event handlers independent
- Error Handling - Wrap handlers in try-catch blocks
- Document Events - Maintain a list of available events
Limitations
- Events do not pass data to callbacks (use closures or jobs instead)
- No event cancellation or stopPropagation
- Debug output is always enabled
- Events are not persisted between requests