Class Details

Callbacks allow the programmer to hook into the life cycle of a Model.

You can control the state of your object by declaring certain methods to be called before or after methods are invoked on your object inside of ActiveRecord.

Valid callbacks are:

  • after_construct: called after a model has been constructed
  • before_save: called before a model is saved
  • after_save: called after a model is saved
  • before_create: called before a NEW model is to be inserted into the database
  • after_create: called after a NEW model has been inserted into the database
  • before_update: called before an existing model has been saved
  • after_update: called after an existing model has been saved
  • before_validation: called before running validators
  • after_validation: called after running validators
  • before_validation_on_create: called before validation on a NEW model being inserted
  • after_validation_on_create: called after validation on a NEW model being inserted
  • before_validation_on_update: see above except for an existing model being saved
  • after_validation_on_update: ...
  • before_destroy: called after a model has been deleted
  • after_destroy: called after a model has been deleted

This class isn't meant to be used directly. Callbacks are defined on your model like the example below:

 class Person extends ActiveRecord\Model {
   static $before_save = array('make_name_uppercase');
   static $after_save = array('do_happy_dance');

   public function make_name_uppercase() {
     $this->name = strtoupper($this->name);

   public function do_happy_dance() {


Available options for callbacks:

  • prepend: puts the callback at the top of the callback chain instead of the bottom

Class Variables

protected static array $VALID_CALLBACKS = array( 'after_construct', 'before_save', 'after_save', 'before_create', 'after_create', 'before_update', 'after_update', 'before_validation', 'after_validation', 'before_validation_on_create', 'after_validation_on_create', 'before_validation_on_update', 'after_validation_on_update', 'before_destroy', 'after_destroy' )

List of available callbacks.

Class Methods

public CallBack __construct ( string $model_class_name )

Creates a CallBack.

  • string $model_class_name - The name of a Model class
public array get_callbacks ( $name $name )

Returns all the callbacks registered for a callback type.

  • return: array of callbacks or null if invalid callback name.
public mixed invoke ( string $model , string $name , [ boolean $must_exist = true] )

Invokes a callback.

  • string $model - Model to invoke the callback on.
  • string $name - Name of the callback to invoke
  • boolean $must_exist - Set to true to raise an exception if the callback does not exist.
  • return: null if $name was not a valid callback type or false if a method was invoked that was for a before_* callback and that method returned false. If this happens, execution of any other callbacks after the offending callback will not occur.
public void register ( string $name , [ mixed $closure_or_method_name = null] , [ array $options = array()] )

Register a new callback.

  • string $name - Name of callback type (see $VALID_CALLBACKS)
  • mixed $closure_or_method_name - Either a closure or the name of a method on the Model
  • array $options - Options array
  • throws: ActiveRecordException if invalid callback type or callback method was not found

The option array can contain the following parameters:

  • prepend: Add this callback at the beginning of the existing callbacks (true) or at the end (false, default)