slim框架调用函数内的函数

时间:2014-04-17 18:45:17

标签: php function slim

我不知道你是否应该把它们称为“功能”但是......

当我的add_record函数被调用时,我想重新使用我所做的另一个函数,它从数据库中获取团队名称列表;

public function add_record($data){

        $teamnames = function get_teamnames;
        print_r($teamnames);
        exit;

        /*

        $this->db->query('INSERT INTO reports(`date`,refereename, refereeteam, hometeam, homecaptain, awayteam) VALUES (

            "'.strtotime($data['date']).'",
            "'.$data['refereename'].'",
            "'.$data['refereeteam'].'",
            "'.$data['hometeam'].'",
            "'.$data['captainname'].'",
            "'.$data['awayteam'].'"
            )');

            */

}

这是我的get_teamnames函数(来自同一个文件)

public function get_teamnames(){
    //Get team data from database, place into resource.
    $teamsData = $this->db->query('SELECT * FROM teamnames ORDER BY teamname ASC');

    // Array which we will return to the Slim router
    $teams = array();

    // PLace all the teams from the mysql resource into the array
    while ($row = $teamsData->fetch_object()) {
      $teams[] = $row;
    }

    // Return the teams array
    return $teams;

}

我想在add_record函数中提供团队名列表的原因是我可以对$data进行一些验证,并确保提交的内容只是存储在数据库中的团队名称之一

1 个答案:

答案 0 :(得分:2)

假设这些函数在同一个类中并且您正在使用该类的对象,只需执行:

$teamnames = $this->get_teamnames();