0

I have a variable $doctype that is an array:

$doctype = array(
    'news' => ('news'),
    'documents' => ('documents'),
    'forms' => ('forms'),
    'other' => ('other'),
);

which is used as options for another array:

$form['doc_type'] = array(
    '#title' => 'Document Type',
    '#type' => 'radios',
    '#options' => $doctype,
    '#size' => '30',
    '#required' => TRUE,
    '#default_value' => $doctype['news'],

now I'm trying to pass the current value for another function

I've tried:

form($form_state, &$doctype) {

but it shows up as a missing argument

I want to pass it through a reference, not as a return (already occupied/don't want to work around it)

Any help is appreciated

2
  • Whether an argument is a reference is controlled by the function definition, not the caller.
    – Barmar
    Commented Jun 21, 2013 at 0:14
  • If you're trying to avoid unnecessary copying, PHP takes care of that for you. It uses copy-on-write, so it only makes a copy if the function modifies the argument.
    – Barmar
    Commented Jun 21, 2013 at 0:15

1 Answer 1

1

Assuming form() specifies its second argument as a reference, then you shouldn't pass &$doctype as a parameter. If the function specifies a parameter as a reference, then all you need to do is pass that variable and it will get passed as a reference.

E.g.

<?php
    function form($var1, &$var2) {
        $var2[2] = 5;
    }
    $var2 = array(1,2,3,4);
    form('test', $var2);
    echo $var2[2]; // Echoes '5';
?>

However, I'm again assuming that form() is built into drupal, in which case as far as I know there's no way of passing $doctype as a reference without changing the core code that defines form()

Out of curiosity, why do you need to pass it as a reference? Can you clarify "already occupied/don't want to work around it"

3
  • I need it passed so when they upload the file, it's directory is based off of what their document type is. The function is already returning $form, and I cant return more than one value without returning an array or an object, and I've tried both of that so I just said that so people dont suggest to do what I've already tried.
    – user2507193
    Commented Jun 21, 2013 at 15:15
  • Also- just referencing the var itself function form($formstate, $doctype) gives me a missing argument error
    – user2507193
    Commented Jun 21, 2013 at 15:18
  • 1
    Where is form() defined? I'm guessing it needs more than two arguments, or you're not defining one of the two. Regardless, given your requirements, you're probably going to need to write your own implementation of form() in order to accomplish what you're trying to do.
    – jraede
    Commented Jun 21, 2013 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.