Re: how to invoke ReflectionMethod and pass variable by reference as argument?



On Fri, 30 May 2008 23:57:20 +0200, rafal.m <rafal.m.malinowski@xxxxxxxxx> wrote:

On 30 Maj, 23:48, Egbert Teeselink <skreb...@xxxxxxxxx> wrote:
> $rc = new ReflectionClass('K2');
> $rm = $rc->getMethod('increment');
> $rm->invoke($a ,$c);

Weird indeed.
Does it work when using call_user_func, or call_user_func_array maybe?
like call_user_func(array($a, 'increment'), $c) or something. it may
be the reflection lib. or i'm missing something.

both:
call_user_func(array($a, 'increment'), $c);
and
call_user_method('increment',$a,$c);
gave me the same outcome:

before: 10
after: 10

As the manual states:
call_user_func():
Note: Note that the parameters for call_user_func() are not passed by reference.
call_user_func_array():
Note: Referenced variables in param_arr are passed to the function by a reference, others are passed by a value. In other words, it does not depend on the function signature whether the parameter is passed by a value or by a reference.

call_user_func_array(array($a,'increment'),array(&$c));

I suspect they have deployed similar code under the hood as call_user_func(_array) in the implementation of the ReflectionMethod invoke()/invokeArgs(). Certainly annoying when the function signature should just be followed IMHO.
--
Rik Wasmus
....spamrun finished
.



Relevant Pages

  • Re: by ref parameter is just a syntax sugar?
    ... In article, Anand ... Hariharan wrote: ... > argument (unless the code reviewer looks up the function signature), ... 'All parameters passed by reference should be 'const' references.' ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Unmanaged C++ object lifetime
    ... Intellisense tells me that the function signature is: ... reference to the push() routine. ... value (which would create a copy of the string). ...
    (microsoft.public.dotnet.languages.vc)