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



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
.



Relevant Pages

  • Re: Script mysteriously stops executing...
    ... memory leaks...that seems to be the case. ... misfortune of being on a corporate-wide PHP RPM that does not include ... more sophisticated memory management tool than the default reference ...   unset; ...
    (comp.lang.php)
  • Re: php5-5.2.11_1 Vulnerabilities
    ... Type of problem: php -- multiple vulnerabilities. ... Security Enhancements and Fixes in PHP 5.2.12 is what the above reference says. ... Info:    PHP Scripting Language ...
    (freebsd-stable)
  • Re: PyQt QCompleter model
    ... Although it may actually modify the list, ... I can save reference to ... model.stringListappend keyword and after pass this reference as an ...   if model.insertRow: ...
    (comp.lang.python)
  • Re: Thread safety help on Hashtable
    ... if I've added a reference object as the value? ... themselves - so modifying fields of those objects does not modify the ...     public IPEndPoint serverEndPoint; ... Server server = new Server; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Add extra function to formula
    ... I need to add reference to a value in another cell also and cannot ... The above looks at cell E3 and if it contains ZEMO returns the value ...   I wish to modify the above formula to only show U3 value ...
    (microsoft.public.excel.worksheet.functions)