Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP - Can I use eval to call a number of functions

Options
  • 30-10-2011 1:15pm
    #1
    Registered Users Posts: 6,501 ✭✭✭


    I am experimenting with adding debug info to the header of a WordPress theme, using the add_action() hook.

    I want to call all the WordPress conditional tags.
    Currently I have code like:
    [php] // WordPress conditional tags.
    echo "WordPress conditional tags information.\n";
    echo ' is_home(): ', is_home() ? 'true' : 'false', "\n";
    echo ' is_front_page(): ', is_front_page() ? 'true' : 'false', "\n";
    echo ' is_page(): ', is_page() ? 'true' : 'false', "\n";
    echo ' is_page_template(): ', is_page_template() ? 'true' : 'false', "\n";
    //etc...[/php]
    Is there a way to use eval in a loop to call these functions?
    Something like:
    [php]$fns = array('is_home', 'is_front_page', 'is_page', 'is_page_template');
    foreach ($fns as $fn) {
    echo " $fn(): ", eval("return ${fn}()") ? 'true' : 'false', "\n";
    }[/php]


Comments

  • Registered Users Posts: 3,140 ✭✭✭ocallagh


    Have a look at array_walk, array_map and call_user_func


  • Registered Users Posts: 6,501 ✭✭✭daymobrew


    ocallagh wrote: »
    Have a look at array_walk, array_map and call_user_func
    call_user_func did the job.
    The code is much shorter now:
    [php] echo "WordPress conditional tags information.\n";
    $conditional_tags = array( 'is_home', 'is_front_page', 'is_page', 'is_page_template', 'is_single',
    'is_singular', 'is_category', 'is_archive', 'is_author', 'is_search', 'is_404',
    'is_paged', 'is_attachment', 'is_feed', 'in_the_loop', 'is_multi_author',
    'is_date', 'is_year', 'is_month', 'is_day', 'is_time',
    'is_tag', 'is_tax', 'is_sticky',
    'is_comments_popup', 'comments_open', 'pings_open',
    'is_admin');
    foreach ($conditional_tags as $cond_tag) {
    echo " $cond_tag(): ", call_user_func($cond_tag) ? 'true' : 'false', "\n";
    }
    // Template tags that do not return true/false.
    $other_tags = array('get_post_type');
    foreach ($other_tags as $cond_tag) {
    echo " $cond_tag(): ", call_user_func($cond_tag), "\n";
    }[/php]


Advertisement