Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

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

  • 30-10-2011 01:15PM
    #1
    Registered Users, Registered Users 2 Posts: 6,652 ✭✭✭


    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, Registered Users 2 Posts: 3,141 ✭✭✭ocallagh


    Have a look at array_walk, array_map and call_user_func


  • Registered Users, Registered Users 2 Posts: 6,652 ✭✭✭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