What are CodeIgniter 4 models' callbacks?
Why Interviewers Ask This
Advanced questions like this reveal whether a candidate has internalized CodeIgniter deeply enough to make architectural decisions. Strong answers demonstrate both breadth and depth of experience.
Answer
CI4 Model callbacks are methods that run automatically before or after database operations — similar to Eloquent model events. Define callback methods in your model and register them: protected $beforeInsert = ["hashPassword", "setCreatedBy"], protected $afterFind = ["formatData"]. Available hooks: beforeInsert, afterInsert, beforeUpdate, afterUpdate, beforeFind, afterFind, beforeDelete, afterDelete. Each callback method receives a $data array containing the operation data and must return the (possibly modified) $data. Example: protected function hashPassword(array $data): array { if (!empty($data["data"]["password"])) { $data["data"]["password"] = password_hash($data["data"]["password"], PASSWORD_BCRYPT); } return $data; }. Callbacks are a clean way to handle cross-cutting concerns like hashing, timestamps, and audit logging.
Pro Tip
Back up your answer with a specific project or situation. Saying 'In my last CodeIgniter project, I used this when...' immediately makes your answer more credible and memorable.
Previous
What is BaseEntity casting in CodeIgniter 4?
Next
What is the difference between CI4 services and Laravel facades?