CodeIgniter Forums
Do i HAVE to use a helper file? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Do i HAVE to use a helper file? (/thread-13701.html)



Do i HAVE to use a helper file? - El Forum - 12-02-2008

[eluser]ryeguy[/eluser]
I want to call validate_username but php says the function is undefined. Do I have to use a helper file to use internal functions or something?

Please ignore the actual code this is my first time using CI I am just figuring out how everything works.

Code:
<?php

class User extends Controller
{
    function add($username, $password)
    {
        $this->load->library('form_validation');
        
        
        $data['variables'] = array("username" => $username,
                                   "password" => hash("sha256", $password),
                                   "user_valid" => validate_username($username));

        $this->load->view('output', $data);
    }
    
    function validate_username($username)
    {
        return
        $this->form_validation->min_length($username,4) &&
        $this->form_validation->max_length($username,15) &&
        $this->form_validation->alpha_numeric($username) &&
        is_string($username[0]);// && //make sure the first character is not a number
    }
}

/* End of file user.php */
/* Location: ./system/application/controllers/user.php */



Do i HAVE to use a helper file? - El Forum - 12-02-2008

[eluser]Michael Wales[/eluser]
You are in an object oriented development environment - you need to tell PHP what class to find that particular function in. In this case, $this works just fine.

Code:
$data['variables'] = array("username"   => $username,
                           "password"   => hash("sha256", $password),
                           "user_valid" => $this->validate_username($username));

On a side note, you probably want to make that function private by prepending an underscore (function _validate_username). Otherwise it is accessible via the URI.