php 调用 Codeigniter 上的未定义函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8966587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:52:28  来源:igfitidea点击:

Call to undefined function on Codeigniter

phpcodeigniterreset

提问by softboxkid

I have the class for resetting a user password. But the code is always gives me an error:

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

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

Here is my code:

这是我的代码:

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;
            }
        }
    }
}

How do I make the method newRandomPwdavailable so it's not undefined?

如何使该方法newRandomPwd可用以使其不是未定义的?

回答by xdazz

newRandomPwd()is not a global function but a object method, you should use $this.

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

Change $newPwd = newRandomPwd();to $newPwd = $this->newRandomPwd();

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