Re: how to invoke ReflectionMethod and pass variable by reference as argument?
- From: "Rik Wasmus" <luiheidsgoeroe@xxxxxxxxxxx>
- Date: Sat, 31 May 2008 00:38:16 +0200
On Fri, 30 May 2008 23:01:39 +0200, rafal.m <rafal.m.malinowski@xxxxxxxxx> 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
.
- Follow-Ups:
- References:
- Prev by Date: Re: how to invoke ReflectionMethod and pass variable by reference as argument?
- Next by Date: Re: following radio & hidden does not work..... help please.
- Previous by thread: Re: how to invoke ReflectionMethod and pass variable by reference as argument?
- Next by thread: Re: how to invoke ReflectionMethod and pass variable by reference as argument?
- Index(es):
Relevant Pages
|