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 17where 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...