我可以在PHP中的另一个函数的参数中声明一个函数吗?

我可以在PHP中的另一个函数的参数中声明一个函数吗?,php,Php,我正在编写帮助函数,使我能够快速构建表。我有一个名为responsive_table的函数,它接受两个参数:标题和行。这些行是动态的,从数据库中提取。我使用foreach循环构建了一个行数组,因为一些db表行与html表的内容或顺序不匹配。。。html表中的某些单元格包含db列等的组合,不能直接从db中显示。“我的HTML表格数组”中没有一行显示。未显示函数参数中声明的函数内部的任何内容。这是我的密码: <?php $output = responsive_table(array('<

我正在编写帮助函数,使我能够快速构建表。我有一个名为responsive_table的函数,它接受两个参数:标题和行。这些行是动态的,从数据库中提取。我使用foreach循环构建了一个行数组,因为一些db表行与html表的内容或顺序不匹配。。。html表中的某些单元格包含db列等的组合,不能直接从db中显示。“我的HTML表格数组”中没有一行显示。未显示函数参数中声明的函数内部的任何内容。这是我的密码:

<?php
$output = responsive_table(array('<input type="checkbox" name="all">', 'Thumbnail', 'Title', 'Location', 'Author', 'Date Submitted', 'Actions'), function(){
    foreach($listing_results as $listing){
        $listings[] = array(
            '<input type="checkbox" name="delete[]" value="'.$listing['listing_id'].'">',
            '<img src="http://placehold.it/50x50" alt="Thumbnail">',
            $listing['title'],
            $listing['location'],
            anchor('members/modify/'.$listing['member_id'], $listing['display_name']),
            date($setting['date_format'], $listing['date_submitted']),
            array(
                array(
                    'title' => 'Modify Listing',
                    'color' => 'primary',
                    'href' => 'listings/modify/'.$listing['listing_id'],
                    'text' => '<i class="fa fa-pencil"></i>'
                ),
                array(
                    'title' => 'Delete Listing',
                    'color' => 'danger',
                    'href' => 'listings/delete/'.$listing['listing_id'],
                    'text' => '<i class="fa fa-eraser"></i>',
                    'confirm' => true
                )
            )
        );
    }
    return $listings;                       
});

 echo $output;

function responsive_table($headings = array(), $rows = array(), $sortable = false){

    $output = '
                                <div class="table-responsive">
                                    <table data-sortable class="table">
                                        <thead>
                                            <tr>
                                                ';

    if(count($headings) > 0){

        foreach($headings as $heading){

            $output .= '
                                                <th>'.$heading.'</th>';

        }

    }

    $output .= '
                                            </tr>
                                        </thead>
                                        <tbody>';

    if(count($rows) > 0){

        foreach($rows as $row){

            $output .= '
                                            <tr>
                                                <td>'.$row.'</td>
                                            </tr>';

        }

    }

    $output .= '
                                    </table>
                                </div>';

    return $output;

}

任何有效的PHP代码都可能出现在函数内部,甚至出现在其他函数和类定义中。看起来您尝试使用的是匿名函数,也称为闭包。这里有一个链接可能会帮助您解决这个问题。

是的,您正在寻找的是一个

因此,当我从我的responsive_table函数中转储$rows变量时,它返回object(Closure)[21]我到底该如何处理它?Lol你在问是否可以将一个函数作为参数插入另一个函数,对吗?闭包就是为此而创建的。假设您执行这个function1($parameter1,$parameter2){echo$parameter2($parameter1);}所以这里发生的事情(假设$parameter2是一个colsure)是将第一个参数传递给调用function1的函数,就像这样:$arg=function($fn2_pm1){//do something};函数1('string',$arg)。希望helpedSo匿名函数与闭包相同,本链接将通过详细示例进一步解释这一点。您有更具体的问题吗?是的,为什么响应_表函数中的$rows变量返回对象(闭包)[21]而不是函数中返回的数组?