2

supposed i define a function like this:

   function   example(&node,$argument2,$argument3){
        ......
         }

could i use this style to invoke the function.

1, example($node,$test1,$test2) //defined function's first argument is passed by reference. but the invoke function is not. could i use this?

2,example(&node,$test1,$test2,$test3,) .// defined function's parameter is three, here is four.could i use this?

zhuanzhou
  • 2,409
  • 8
  • 33
  • 51

5 Answers5

3

1,

 example($node,$test1,$test2);

This will still pass the first parameter by reference, because the function definition states so. You don't need to use any special syntax here.

So to answer the could I use it - Yes. That's actually how you should use it.

2,

 example($node,$test1,$test2,$test3)

I assume this was a typo. The first parameter should still be noted as $node, not &node.

And it will still be passed as reference without any extra syntax.

The fourth parameter ($test3) will however be unknown within your original funciton. It will not have a name, because you didn't define the 4th param in you func declaration. In that case it will only be available as:

 $test3 = func_get_arg(3);

If you want to pass any of the other parameters by reference, then you can use the special pass-by-ref on invocation syntax:

 example($node, & $test1, & $test2);

This will give you an E_DEPRECATED warning on most setups. But it would still work as intended.

You only need the func( & $var ) syntax if you want to override what the function intended to be passed by reference per default.

mario
  • 144,265
  • 20
  • 237
  • 291
1

You should use first call example($node,$test1,$test2)

call time pass by reference has been deprecated. so the second method will generate PHP warning message.

EDIT

See this question on SO PHP warning: Call-time pass-by-reference has been deprecated

Community
  • 1
  • 1
Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
  • The mismatch of the parameter count is not an error, much less will it "not work". It simply ends up as anonymous parameter in `func_get_args` – mario Apr 22 '11 at 06:26
1

you can use this

example($node,$test1,$test2)

but not point 2 because the number of parameters dont match...you can do that by function overloading

Dharmesh
  • 1,039
  • 1
  • 12
  • 17
0

There's a native PHP function called func_get_args(). Here:

http://php.net/manual/en/function.func-get-args.php

That should do the trick.

Will Martin
  • 4,142
  • 1
  • 27
  • 38
0

IN PHP YOU WONT TO PASS BY reference

example(&$node,$test1,$test2,$test3,)

function add_some_extra(&$string)
{

    $string .= 'and something extra.';

}
$str = 'This is a string, ';

add_some_extra($str);

echo $str;    // outputs 'This is a string, and something extra.'
jayesh
  • 2,422
  • 7
  • 44
  • 78