PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > alt.php > Howto re-pass arguments?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Howto re-pass arguments?

Réponse
 
LinkBack Outils de la discussion
Vieux 21/10/2007, 07h48   #1
zapzap
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Howto re-pass arguments?

Hello :-)

Is it possible to pass all arguments to another function?

function A()
{
B(func_get_args());
}

function B()
{
var_dump(func_get_args());
}


I dont know how many arguments will come for A, and i want
A to handle them transparent and pass all to B.
Now i'm doing this with func_get_args but I get
an array ob B's input. original arguments would be
better. Is this possible in any way?

TIA 8-)
  Réponse avec citation
Vieux 21/10/2007, 08h11   #2
J.O. Aho
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Howto re-pass arguments?

zapzap wrote:
> Hello :-)
>
> Is it possible to pass all arguments to another function?
>
> function A()
> {
> B(func_get_args());
> }
>
> function B()
> {
> var_dump(func_get_args());
> }
>
>
> I dont know how many arguments will come for A, and i want
> A to handle them transparent and pass all to B.
> Now i'm doing this with func_get_args but I get
> an array ob B's input. original arguments would be
> better. Is this possible in any way?


function b($array) {
if(is_array($array)) {
foreach($array AS $var) {
var_dump($var);
}
} else {
var_dump($array);
}
}


--

//Aho
  Réponse avec citation
Vieux 21/10/2007, 08h22   #3
zapzap
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Howto re-pass arguments?

J.O. Aho wrote:
> zapzap wrote:
>> Hello :-)
>> Is it possible to pass all arguments to another function?
>> function A()
>> {
>> B(func_get_args());
>> }
>>
>> function B()
>> {
>> var_dump(func_get_args());
>> }
>> I dont know how many arguments will come for A, and i want
>> A to handle them transparent and pass all to B.
>> Now i'm doing this with func_get_args but I get
>> an array ob B's input. original arguments would be
>> better. Is this possible in any way?

> function b($array) {
> if(is_array($array)) {
> foreach($array AS $var) {
> var_dump($var);
> }
> } else {
> var_dump($array);
> }
> }


No no no ;-) My fault, see this:

//
function A()
{
B(); // THIS NEEDS TO BE CHANGED PROBLY :>
}

//
function B()
{
var_dump(func_get_args());
}


So, I will NEVER call B directly, i'll always call A
an A will call B for me.

B is loaded/generated dynamicaly, so one time B requires
2 arguments, sometimes 5 arguments. As i said i will
only call A, sometimes A($var1,$var2,$var3), other
A($var1). How can I tell A to pass all its
arguments to B - WITHOUT having them as an array (as result
of fun_get_args in my previous posting).

I hope this is clear now ;-) sorry for my english
  Réponse avec citation
Vieux 21/10/2007, 08h46   #4
J.O. Aho
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Howto re-pass arguments?

zapzap wrote:
> J.O. Aho wrote:
>> zapzap wrote:
>>> Hello :-)
>>> Is it possible to pass all arguments to another function?
>>> function A()
>>> {
>>> B(func_get_args());
>>> }
>>>
>>> function B()
>>> {
>>> var_dump(func_get_args());
>>> }
>>> I dont know how many arguments will come for A, and i want
>>> A to handle them transparent and pass all to B.
>>> Now i'm doing this with func_get_args but I get
>>> an array ob B's input. original arguments would be
>>> better. Is this possible in any way?

>> function b($array) {
>> if(is_array($array)) {
>> foreach($array AS $var) {
>> var_dump($var);
>> }
>> } else {
>> var_dump($array);
>> }
>> }

>
> No no no ;-) My fault, see this:
>
> //
> function A()
> {
> B(); // THIS NEEDS TO BE CHANGED PROBLY :>
> }
>
> //
> function B()
> {
> var_dump(func_get_args());
> }
>
>
> So, I will NEVER call B directly, i'll always call A
> an A will call B for me.


If you want the arguments that are used when you call function A(), you need
to use func_get_args() in function A(), so you would need to use
b(func_get_args()). As you see from b(), it won't present your variables
inside a array.

--

//Aho
  Réponse avec citation
Vieux 21/10/2007, 08h57   #5
zapzap
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Howto re-pass arguments?

J.O. Aho wrote:
> If you want the arguments that are used when you call function A(), you need
> to use func_get_args() in function A(), so you would need to use
> b(func_get_args()). As you see from b(), it won't present your variables
> inside a array.


Ok, thanks :-) Array is not bad, but I thought I could
do this in a more "transparent" way, without "touching"
the arguments.
  Réponse avec citation
Vieux 21/10/2007, 12h55   #6
Thomas Mlynarczyk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Howto re-pass arguments?

Also sprach zapzap:

> Is it possible to pass all arguments to another function?


Yes: http://de2.php.net/call_user_func_array.

function A()
{
$tmp = func_get_args();
return call_user_func_array( 'B', $tmp );
}

function B() { /* do something */ }

BTW, you cannot pass func_get_args() directly as a parameter to
call_user_func_array(), as PHP will get confused and report an error.
Therefore, it is necessary to go via the $tmp variable.

Greetings,
Thomas

--
C'est pas parce qu'ils sont nombreux a avoir tort qu'ils ont raison!
(Coluche)


  Réponse avec citation
Vieux 21/10/2007, 15h08   #7
zapzap
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Howto re-pass arguments?

Thomas Mlynarczyk wrote:
> Also sprach zapzap:
>> Is it possible to pass all arguments to another function?

> Yes: http://de2.php.net/call_user_func_array.
> function A()
> {
> $tmp = func_get_args();
> return call_user_func_array( 'B', $tmp );
> }
> function B() { /* do something */ }
> BTW, you cannot pass func_get_args() directly as a parameter to
> call_user_func_array(), as PHP will get confused and report an error.
> Therefore, it is necessary to go via the $tmp variable.


Great! Thanks! Danke! This solves my problem permanently 8-)

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h42.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11610 seconds with 15 queries