开发者

Should this function be public or private?

I am verifying the email in database in form validation in CodeIgniter by using callback in rule. For example

$this -> form_validation -> set_rules( 'email', 'Email address', 'trim|valid_email|callback_email_exists' );

The email_exists function is:

public function email_exists($email)
    {
        $this -> load -> model('account_model');
        $exists = $this -> account_model -> email_registered( $email );
        if ( $exists == true )
        {
            $this -> form_validation -> set_message ( 'email_exists', 'Email already exists.');
            return false;
        }
        return true;        
    }

It works fine. However, shouldn't the above email_exists function be a private function instead of public? I try to make it private like private function _email_exists($email) and i call back it by callback__email_exists

However I get the error:

Fatal error: Call to private method Account::_email_exists() from context 'CI_Form_validation' in ....(line number)

Can so开发者_JS百科meone tell me what's wrong?


If you need to call it from outside the object (whether as callback or directly) it should be public


You can do so:

$rules['field'] = 'callback__email_exists';
$this->validation->set_rules($rules);

function _email_exists() {
  // Normal callback function rules
} 

By sure to append two underscores to the callback instead of just one.

Don't forget to add the underscore to the message rule as well:

$this->validation->set_message('_email_exists', 'The email already exists');


You are validating the email, on the basis of your model account_model, so this makes the function more centered to the private boundary. You will not achieve anything significant, by making this function public, instead you can make this function what it is

A PRIVATE FUNCTION TO CHECK EMAILS FOR MODEL account_model
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜