Check alle échte Black Friday-deals Ook zo moe van nepaanbiedingen? Wij laten alleen échte deals zien

[php] n variabelen doorgeven, maar niet als array

Pagina: 1
Acties:

  • Rekcor
  • Registratie: Februari 2005
  • Laatst online: 08-10 13:03
Ik wil graag alle variabelen doorgeven van de ene functie naar de andere, maar niet als een array.

PHP:
1
2
3
4
$this->requestHandlers[$methodName] = function() use ($databaseConnector, $methodName) {
  $args = func_get_args();
  return call_user_func_array(array($databaseConnector, $methodName), array(&$args));
};


en verderop in de databaseConnector class:

PHP:
1
2
3
 public function test($a, $b) {
   echo $a . '-' . $b;
 }


Als ik vervolgens:

PHP:
1
2
3
4
5
6
$this->requestHandlers['test'] = function() use ($databaseConnector, 'test') {
  $args = func_get_args();
  return call_user_func_array(array($databaseConnector, 'test'), array(&$args));
};

$this->requestHandlers['test']('a', 'b');


Krijg ik

code:
1
Array-Array


wat logisch is.

Echter, ik wil dat alle variabelen gewoon als losse variabelen binnenkomen bij de test-functie. Klopt het dat dit niet mogelijk is?

  • Creepy
  • Registratie: Juni 2001
  • Laatst online: 15:09

Creepy

Tactical Espionage Splatterer

Je $args is al een array, waarom zou je die dan nogmaals in een array stoppen in call_user_func_array? Kan je niet direct
PHP:
1
 return call_user_func_array(array($databaseConnector, 'test'), $args);
doen? Of een nieuwe array aanmaken aan de hand van $args.

[ Voor 10% gewijzigd door Creepy op 14-11-2013 14:40 ]

"I had a problem, I solved it with regular expressions. Now I have two problems". That's shows a lack of appreciation for regular expressions: "I know have _star_ problems" --Kevlin Henney


  • Rekcor
  • Registratie: Februari 2005
  • Laatst online: 08-10 13:03
Bliksem, je hebt gelijk! Bedankt Creepy!

  • Rekcor
  • Registratie: Februari 2005
  • Laatst online: 08-10 13:03
PHP:
1
2
3
4
5
<?php
$this->requestHandlers[$methodName] = function() use ($databaseConnector, $methodName) {
  return call_user_func_array(array($databaseConnector, $methodName), func_get_args());
};
?>