0

Given that in PHP5 objects are said to be passed by reference automatically, are there reasons why one must not pass an object through forms ('programmatically')? I hope to create acceptable persistence through using $form_state for use with AJAX or form submit handlers. I've tried the following and it works (but I don't want to be doing something in poor practice/ that might get blocked).

$foo = new stdClass();
$foo->bar = 'foobar';

$form['foo-bar'] = array(
    '#type' => 'value',
    '#value' => $foo,
);

Which then appears in $form_state when the following element triggers an AJAX request

$form['numbers_required'] = array(
    '#type' => 'textfield',
    '#title' => t('Numbers needed'),
    '#ajax' => array(
        'callback' => 'numbers_required_callback',
        'wrapper' => 'numbers_remaining',
    ),
);

Furthermore, the following passage from the drupal_build_form() documentation suggests I might have to set the form 'cache' property to TRUE. Perhaps for others this is the case if they aren't using an AJAX callback. Given that I am, I assume I can ignore this, but thought others might find it useful to know.

cache: If set to TRUE the original, unprocessed form structure will be cached, which allows the entire form to be rebuilt from cache. A typical form workflow involves two page requests; first, a form is built and rendered for the user to fill in. Then, the user fills the form in and submits it, triggering a second page request in which the form must be built and processed. By default, $form and $form_state are built from scratch during each of these page requests. Often, it is necessary or desired to persist the $form and $form_state variables from the initial page request to the one that processes the submission. 'cache' can be set to TRUE to do this. A prominent example is an Ajax-enabled form, in which ajax_process_form() enables form caching for all forms that include an element with the #ajax property. (The Ajax handler has no way to build the form itself, so must rely on the cached version.) Note that the persistence of $form and $form_state happens automatically for (multi-step) forms having the 'rebuild' flag set, regardless of the value for 'cache'.

1

Objects are 'passed by reference' in the context of being passed to a function in a single page request.

Once the page has reloaded (e.g. when you submit your form), the memory for the original object is freed and it's impossible to maintain a reference to it (at least with standard PHP to the best of my knowledge).

You can keep a copy of the object (which is what Drupal does), and instantiate a version of it on the next page, one that has exactly the same characteristics as the original even, but it's not a reference to it.

What you're doing is absolutely fine, but if you're worried about changes being made to the object after it's been attached to a form, but before it's been submitted, then you should keep hold of the object's identifier instead, and use whatever helper function is available to re-load that object in the submission process. That would be safest I think.

But if your class is dumb, and doesn't have DB persistence/unique identifiers/something that's going to be changed by some other part of the system, then just add it as a value as you're doing already.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.