Why doesn't passing a link by reference work when calling the reflexive method?

My function, prepare (), has a definition:

preparing a private function (& $ data, $ conditions = null, $ ConditionsRequired = false)

When I test it

/** * @covers /data/DB_Service::prepare * @uses /inc/config */ public function testNoExceptionIsRaisedForValidPrepareWithConditionsAndConditionsRequiredArguments() { $method = new ReflectionMethod('DB_Service', 'prepare'); $method->setAccessible(TRUE); $dbs = new DB_Service(new Config(), array('admin', 'etl')); $data = array('message' => '', 'sql' => array('full_query' => "")); $method->invoke($dbs, $data, array('conditionKey' => 'conditionValue'), TRUE); } 

picks up (and interrupts my test)

ReflectionException: a call to the DB_Service :: prepare () method failed

However this

  /** * @covers /data/DB_Service::prepare * @uses /inc/config */ public function testNoExceptionIsRaisedForValidPrepareWithConditionsAndConditionsRequiredArguments() { $method = new ReflectionMethod('DB_Service', 'prepare'); $method->setAccessible(TRUE); $dbs = new DB_Service(new Config(), array('admin', 'etl')); //$data is no longer declared - the array is directly in the call below $method->invoke($dbs, array('message' => '', 'sql' => array('full_query' => "")), array('conditionKey' => 'conditionValue'), TRUE); } 

works fine and the test was successful.

Why does declaring a variable and then passing does not work, but just creating it in a method call works? I think this has something to do with how invoke () works, but I can't figure that out.

+6
source share
1 answer

From the documentation for invoke :

Note. If a function has arguments that must be links, they must be links in the list of passed arguments.

So your first example should work if you change it to:

 $method->invoke($dbs, &$data, array('conditionKey' => 'conditionValue'), TRUE); 

EDIT: To avoid stale link time, you can use an array and invokeArgs :

 $method->invokeArgs($dbs, array(&$data, array('conditionKey' => 'conditionValue'), TRUE)); 
+8
source

Source: https://habr.com/ru/post/976090/


All Articles