How to use Form API when the form callback is a method? How can I use AClass::FormCallback() as $valid_callback on the code below?

class AClass {
  public function FormCallback() {
  }
}

$valid_callback = ?

drupal_get_form($valid_callback);
drupal_retrieve_form() uses function_exists() before calling the form builder, and that function doesn't work if you pass a string such as 'AClass::FormCallback'. If you pass it an array, e.g. array('AClass', 'FormCallback'), you get an error about the function expecting a string.

What you can do is to use a custom function containing code similar to the following one, and use it as form builder.

function mymodule_form_builder($form, &amp;$form_state) {
  $class = $form['#class_callback'];
  $method = $form['#method_callback'];

  if (method_exists($class, $method)) {
    $args = array_merge(array($form, &amp;$form_state), $form_state['build_info']['args']);
    $form = call_user_func_array(array($class, $method), $args);

    return $form;
  }
}


The code is simplified, and it doesn't contain code to check any possible error condition. It works also just in the case of static methods.

For an explanation of the values passed in $form_state, see the documentation for drupal_build_form().