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



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
.



Relevant Pages

  • Re: Dynamically load functions
    ... $includeMap should contain 1 entry for each function I wish to reference, ... Is the function 'call_user_func_array' the actual system function calling routine? ... public function __call{ ...
    (comp.lang.php)
  • Re: Function Warning - Null Reference
    ... Warning 1 Function 'Dec2hms' doesn't return a value on all code paths. ... null reference exception could occur at run time when the result is used. ... Public Function Dec2hmsAs String ... Public Function Dec2hms As String ...
    (microsoft.public.dotnet.languages.vb)
  • RE: Run-time Error 2186
    ... I should also point out that there are other buttons that also reference the ... When I obtain the "Run-time Error 2186" and hit debug, ... Public Function Manufacturer() As String ...
    (microsoft.public.access.forms)
  • Re: Call Private Function from ThisWorkbook module
    ... The way you describe is how you reference a Public function in a class ... Bob Phillips ... >> Beto, ...
    (microsoft.public.excel.programming)
  • Re: Simple Multiplication Formula
    ... It produces an "absolute" reference ... oWs As Worksheet) As Boolean ... I'm trying to write a function that will put a formula in ResultCol to ... Public Function MultiplyCells(RowIdx As Long, ColOne As Long, ColTwo As ...
    (microsoft.public.excel.programming)