Re: how to invoke ReflectionMethod and pass variable by reference as argument?
- From: "rafal.m" <rafal.m.malinowski@xxxxxxxxx>
- Date: Fri, 30 May 2008 16:32:55 -0700 (PDT)
On 31 Maj, 00:38, "Rik Wasmus" <luiheidsgoe...@xxxxxxxxxxx> wrote:
On Fri, 30 May 2008 23:01:39 +0200, rafal.m <rafal.m.malinow...@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
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
.
- References:
- Prev by Date: Re: following radio & hidden does not work..... help please.
- Next by Date: Re: include 'filename.php' vs. exit 'whatever'
- Previous by thread: Re: how to invoke ReflectionMethod and pass variable by reference as argument?
- Next by thread: following radio & hidden does not work..... help please.
- Index(es):
Relevant Pages
|