1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Check for unused functions in PHP

Discussion in 'PHP' started by THT, Aug 3, 2005.

  1. #1
    Is there a program that can check for unused functions within a PHP file?

    Ive seen one for ASP, but i need a PSP port
     
    THT, Aug 3, 2005 IP
  2. mck9235

    mck9235 Peon

    Messages:
    42
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What exactly do you mean, unused functions? Do you mean a function thats in the script but isn't executed? :confused:
     
    mck9235, Aug 3, 2005 IP
  3. THT

    THT Peon

    Messages:
    686
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    yeah an function in the script which is never called
     
    THT, Aug 3, 2005 IP
  4. dave487

    dave487 Peon

    Messages:
    701
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #4
    how about setting up a list of variables at the top of the page like
    $script1="unused";
    $script2="unused";
    etc

    and then when each funtion is called change the variable to "used" somewhere within the function.

    At the end of the page you can output all the functions which are still at the "unused" stage.
     
    dave487, Aug 4, 2005 IP
  5. mck9235

    mck9235 Peon

    Messages:
    42
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Nice idea Dave! It'd probaly be easier to use an array to set it up though, just less typing. :p
    E.g:
    PHP:
    1.  
    2. <?php
    3. $functions = array("unused", "unused", "unused");
    4. //Use a function
    5. if(strlen("goose") == 5)
    6. {
    7.    echo("Woo, its five characters.");
    8.    //set the first array value to used
    9.    $functions[0] = "used";
    10. }
    11. //Echo out the array at the end of output
    12. echo $functions;
    13. ?>
    14.  
    Credit goes to Dave for the idea, not sure if the above will work though. Good luck.
     
    mck9235, Aug 5, 2005 IP
  6. rvarcher

    rvarcher Peon

    Messages:
    69
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    This might be a little cleaner....

    Put this at the beginning of each function:
    PHP:
    1.  
    2.           $GLOBALS['functions'][__FUNCTION__]++;
    3.  
    Then after your script is over run this:
    PHP:
    1.  
    2.      foreach ( $GLOBALS['functions'] as $function_name => $times_called )
    3.      {
    4.  
    5.           echo "\n<br>$function_name</b> - $times_called";
    6.      }
    7.  
    Which should output something like this:
    PHP:
    1.  
    2. get_form_input - 1
    3. set_where - 1
    4. get_promo_code - 2
    5. get_user_location - 1
    6.  
     
    rvarcher, Aug 5, 2005 IP
  7. daed

    daed Peon

    Messages:
    93
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    That seems like a lot of code modification and trouble to go through just to find functions that are unused? Surely if you have to manually go through that many lines of code it would be obvious what the script itself is doing, and what functions it doesn't need.

    Maybe you could post the code as an attachment and have someone look it over? Perhaps in some situations, a function will never be called on certain conditions.. whereas other times, the function is necessary to perform the action it was originally intended.
     
    daed, Aug 7, 2005 IP
  8. THT

    THT Peon

    Messages:
    686
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #8
    THT, Aug 7, 2005 IP
  9. daed

    daed Peon

    Messages:
    93
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    daed, Aug 7, 2005 IP