在Codeigniter上调用未定义的函数

时间:2012-01-23 02:19:08

标签: php codeigniter reset

我有重置用户密码的课程。但代码总是给我一个错误:

Fatal error: Call to undefined function newRandomPwd() in 
C:\AppServ\www\phonebook\application\controllers\reset.php 
on line 32

这是我的代码:

class Reset extends CI_Controller{
    function index(){
        $this->load->view('reset_password');
    }
    function newRandomPwd(){
        $length = 6;
        $characters = 'ABCDEF12345GHIJK6789LMN$%@#&';
        $string = '';    

        for ($p = 0; $p < $length; $p++) {
            $string .= $characters[mt_rand(0, strlen($characters))];
        }
        return $string;
    }
    function resetPwd(){

        $newPwd = newRandomPwd();                   //line 32, newRandomPwd() 
                                                    //is undefined

        $this->load->library('form_validation');
        $this->load->model('user_model');
        $getUser = $this->user_model->getUserLogin();
        if($getUser)
        {
            $this->user_model->resetPassword($newPwd);
            return TRUE;
        } else {
            if($this->form_validation->run()==FALSE)
            {
                $this->form_validation->set_message('','invalid username');
                $this->index();
                return FALSE;
            }
        }
    }
}

如何使方法newRandomPwd可用,因此未定义?

1 个答案:

答案 0 :(得分:22)

newRandomPwd()不是全局函数,而是对象方法,您应该使用$this

$newPwd = newRandomPwd();更改为$newPwd = $this->newRandomPwd();