473,503 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to invoke ReflectionMethod and pass variable by reference asargument?

Hi

This code fails:

class K2 {

public function increment(&$obj) {
$obj += 1;
}

}

$a = new K2();
$c = 10;
echo "before: $c <br/>\n";

$rc = new ReflectionClass('K2');
$rm = $rc->getMethod('increment');
$rm->invoke($a ,$c);

echo "after: $c <br/>\n";
and the outcome is:

Fatal error: Uncaught exception 'ReflectionException' with message
'Invocation of method K2::increment() failed' in /temptest.php:17
Stack trace: #0 /temptest.php(17): ReflectionMethod-
>invoke(Object(K2), 10) #1 {main} thrown in /temptest.php on line 17
where line 17 is : $rm->invoke($a ,$c);

but if i change line 5:
from
public function increment(&$obj)
to

public function increment($obj)

it will work fine, but the echo before and after will the same :) (no
$c increasing)

of course if change line 17
from
$rm->invoke($a ,$c);
to
$rm->invoke($a ,&$c);

i will have outcome
before $c == 10
after $c == 11

but also php engine will throw new worrning:
Warning: Call-time pass-by-reference has been deprecated; If you would
like to pass it by reference, modify the declaration of [runtime
function name](). If you would like to enable call-time pass-by-
reference, you can set allow_call_time_pass_reference to true in your
INI file. in /temptest.php on line 17

I can write some wrapper to send variables by reference but it wont be
clean solution.. do you have any other ideas?

thanks for your time
rafal

ps. I also don't want to change allow_call_time_pass_reference = Off
in php.ini file...

Jun 2 '08 #1
5 4147
$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.
Jun 2 '08 #2
On 30 Maj, 23:48, Egbert Teeselink <skreb...@gmail.comwrote:
$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

r.
Jun 2 '08 #3
On Fri, 30 May 2008 23:01:39 +0200, rafal.m <ra****************@gmail.com
wrote:
Hi

This code fails:

class K2 {

public function increment(&$obj) {
$obj += 1;
}

}

$a = new K2();
$c = 10;
echo "before: $c <br/>\n";

$rc = new ReflectionClass('K2');
$rm = $rc->getMethod('increment');
$rm->invoke($a ,$c);

echo "after: $c <br/>\n";
and the outcome is:

Fatal error: Uncaught exception 'ReflectionException' with message
'Invocation of method K2::increment() failed' in /temptest.php:17
Stack trace: #0 /temptest.php(17): ReflectionMethod-
>invoke(Object(K2), 10) #1 {main} thrown in /temptest.php on line 17

where line 17 is : $rm->invoke($a ,$c);

but if i change line 5:
from
public function increment(&$obj)
to

public function increment($obj)

it will work fine, but the echo before and after will the same :) (no
$c increasing)

of course if change line 17
from
$rm->invoke($a ,$c);
to
$rm->invoke($a ,&$c);

i will have outcome
before $c == 10
after $c == 11

but also php engine will throw new worrning:
Warning: Call-time pass-by-reference has been deprecated; If you would
like to pass it by reference, modify the declaration of [runtime
function name](). If you would like to enable call-time pass-by-
reference, you can set allow_call_time_pass_reference to true in your
INI file. in /temptest.php on line 17

I can write some wrapper to send variables by reference but it wont be
clean solution.. do you have any other ideas?
It's sad we have to resort to trickery, but here it is:
<?php
class K2 {
public function increment(&$obj) {
$obj += 1;
}
}

$a = new K2();
$c = 10;
echo "before: $c <br/>\n";
$rc = new ReflectionClass('K2');
$rm = $rc->getMethod('increment');
$rm->invokeArgs($a ,array(&$c));
echo "after: $c <br/>\n";
?>
--
Rik Wasmus
....spamrun finished
Jun 2 '08 #4
On Fri, 30 May 2008 23:57:20 +0200, rafal.m <ra****************@gmail.com
wrote:
On 30 Maj, 23:48, Egbert Teeselink <skreb...@gmail.comwrote:
$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
Jun 2 '08 #5
On 31 Maj, 00:38, "Rik Wasmus" <luiheidsgoe...@hotmail.comwrote:
On Fri, 30 May 2008 23:01:39 +0200, rafal.m <rafal.m.malinow...@gmail.com>*
wrote:
Hi
This code fails:
class K2 {
* *public function increment(&$obj) {
* * * * * *$obj += 1;
* *}
}
$a = new K2();
$c = 10;
echo "before: $c <br/>\n";
$rc = new ReflectionClass('K2');
$rm = $rc->getMethod('increment');
$rm->invoke($a ,$c);
echo "after: $c <br/>\n";
and the outcome is:
Fatal error: Uncaught exception 'ReflectionException' with message
'Invocation of method K2::increment() failed' in /temptest.php:17
Stack trace: #0 /temptest.php(17): ReflectionMethod-
invoke(Object(K2), 10) #1 {main} thrown in /temptest.php on line 17
where line 17 is : $rm->invoke($a ,$c);
but if i change line 5:
from
* *public function increment(&$obj)
to
* *public function increment($obj)
it will work fine, but the echo before and after will the same :) (no
$c increasing)
of course if change line 17
from
* *$rm->invoke($a ,$c);
to
* *$rm->invoke($a ,&$c);
i will have outcome
before $c == 10
after $c == 11
but also php engine will throw new worrning:
Warning: Call-time pass-by-reference has been deprecated; If you would
like to pass it by reference, modify the declaration of [runtime
function name](). If you would like to enable call-time pass-by-
reference, you can set allow_call_time_pass_reference to true in your
INI file. in /temptest.php on line 17
I can write some wrapper to send variables by reference but it wont be
clean solution.. do you have any other ideas?

It's sad we have to resort to trickery, but here it is:
<?php
class K2 {
* * * * public function increment(&$obj) {
* * * * * * * * $obj += 1;
* * * * }

}

$a = new K2();
$c = 10;
echo "before: $c <br/>\n";
$rc = new ReflectionClass('K2');
$rm = $rc->getMethod('increment');
$rm->invokeArgs($a ,array(&$c));
echo "after: $c <br/>\n";
?>
--
Rik Wasmus
...spamrun finished
so it is not a bug :) it is the feature of php ;)

thanks :) nice and easy :)
I don't know why I didn't think about it ;D
regards
rafal
Jun 2 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
9251
by: Randell D. | last post by:
Folks, I've asked this before but never got any response but its important and I thought I'd pitch it again (hopefully a bit clearer)... One can pass data from one function to another either...
1
401
by: Walter Zydhek | last post by:
Can anyone help with the use of the Invoke method of a Directory Entry object? I am using vb.net. I am trying to invoke the get_Filter method of the IIS://LocalHost directory entry, and get the...
4
3392
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then...
16
2469
by: Duncan Mole | last post by:
Hi, This is probably an easy one but it iy first bit of p/invoke. I am trying to use the following C struct in a call: typedef struct { BYTE SRB_Cmd; BYTE SRB_Status, BYTE ...
14
7332
by: stic | last post by:
Hi, I'm in a middle of writing something like 'exception handler wraper' for a set of different methodes. The case is that I have ca. 40 methods form web servicem, with different return values...
6
1330
by: Boni | last post by:
Dear sirs/madam, I am trying to dynamically invoke assembly. (For some days Tomas helped me to get it compiled). Now I am so far that I can test if the assembly invoked. And I get the...
10
13607
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the...
12
2995
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
275
12035
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7458
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5013
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.