How to get parameter names from defined function

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default How to get parameter names from defined function

    Dear develoopers,

    I know i can reveal the defined functions with:

    <?php
    function myrow($id, $data) {
    return "<tr><th>$id</th><td>$data</td></tr>\n";
    }

    $arr = get_defined_functions();

    print_r($arr);
    ?>


    But consider the next function:

    <?php

    function myfunction($para1, $para2, $para3) {
    ..
    //do something with the paramaters
    ..
    }
    ?>


    How can I get the parameter names i.e. $para1, $para2, $para3?

    Any help would be highly apreciated.

    Kind regards,

    DutchFish


    DutchFish Guest

  2. Similar Questions and Discussions

    1. #39161 [NEW]: parameter names not sent to prepared statement
      From: aspen dot olmsted at alliance dot biz Operating system: windows xp sp 2 PHP version: 5CVS-2006-10-15 (snap) PHP Bug Type: ...
    2. #38951 [Opn->Bgs]: ftp_ssl_connect() function not defined
      ID: 38951 Updated by: derick@php.net Reported By: vinnie0921 at hotmail dot com -Status: Open +Status: ...
    3. User defined function causes empty result set
      I have created a UDF that takes an INTEGER as parameter and returns a VARCHAR. Whenever I try to use the function in a SELECT, it causes the SELECT...
    4. note 33974 added to function.defined
      btw: (PHP 4.1.2 + 4.3.2) echo gettype(defined('SOMETHING')); integer ---- Manual Page -- http://www.php.net/manual/en/function.defined.php...
    5. note 33633 added to function.defined
      Pour la traduction francaise, ne pas lire ``defined -- (PHP 3, PHP 4 ) defined -- Vérifie l'existence d'une fonction Description bool defined...
  3. #2

    Default Re: How to get parameter names from defined function


    "DutchFish" <salesvast@home.nl> schreef in bericht
    news:bsjnfn$ut1$1@news2.tilbu1.nb.home.nl...
    >
    > How can I get the parameter names i.e. $para1, $para2, $para3?
    >
    You can't, because they aren't important. The passed values are, and they
    can be retrieved by using func_get_args:

    [url]http://www.php.net/manual/en/function.func-get-args.php[/url]


    JW



    Janwillem Borleffs Guest

  4. #3

    Default Re: How to get parameter names from defined function

    "DutchFish" <salesvast@home.nl> wrote in message
    news:bsjnfn$ut1$1@news2.tilbu1.nb.home.nl...
    > Dear develoopers,
    >
    > But consider the next function:
    > <?php
    > function myfunction($para1, $para2, $para3) {
    > //do something with the paramaters
    > }
    > ?>
    >
    > How can I get the parameter names i.e. $para1, $para2, $para3?
    > Any help would be highly apreciated.
    > Kind regards,
    > DutchFish
    First, is your question, what is the name of the variables that were passed
    to the function?
    if so here you go, a little wierd, but I do it like this, (untill i can find
    something better)

    $myvar = "John Doe";
    $othervar = "Jane";

    someFunction($myvar,$othervar);

    function someFunction($para1,$para2)
    {
    $v1 = array_search ($para1,$GLOBALS);
    print "para1 came from var: $v1 \n";

    $v2 = array_search ($para2,$GLOBALS);
    print "para2 came from var: $v2 \n";

    }

    ----- this is what it spits out

    para1 came from var: myvar
    para2 came from var: othervar



    Mike Bradley
    [url]http://gzen.myhq.info[/url] -- free online php tools



    CountScubula Guest

  5. #4

    Default Re: How to get parameter names from defined function

    you can do it like this:

    <?php

    function myfunction($para1, $para2, $para3) {
    foreach (get_defined_vars() as $name=>$value)
    {
    echo $name;
    }
    }
    ?>
    DJP Guest

  6. #5

    Default How to get parameter names from defined function

    You can't, because they aren't important. The passed values are, and they
    can be retrieved by using func_get_args:

    You can, and variable names can be important as they act as keys in the KVP structure of the language. See response #4 for the right answer.
    Unregistered Guest

  7. #6

    Default Re: How to get parameter names from defined function

    'important' is a judgement call, no one cares what you think if you're not helping to answer the question.

    Here's how:

    $reflector = new ReflectionFunction('test');
    $params = array();
    foreach ($reflector->getParameters() as $param) {
    $params[] = $param->name;
    }
    print_r($params);

    http://stackoverflow.com/questions/3515406/how-to-determine-user-defined-function-parameters-names-before-calling-it
    Unregistered Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139