#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,660
    Rep Power
    171

    Combine multiple functions?


    Hi;

    Would it be a good idea to combine simular functions to save space?

    Or is it best to keep them seperate?

    For example I have many functions like these
    PHP Code:
    public function validate_city($city)
            {
                if(
    $city == '0')
                    {
                        
    $this->form_validation->set_message('validate_city''Please select a city');
                        return 
    false;
                    }
                else
                    {
                        return 
    true;
                    }    
            }

        public function 
    validate_suburb($suburb)
            {
                if(
    $suburb == '0')
                    {
                        
    $this->form_validation->set_message('validate_suburb''Please select a suburb');
                        return 
    false;
                    }
                else
                    {
                        return 
    true;
                    }
            } 
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    you should definitely reduce that to a single function, because the logic is always the same:

    PHP Code:
    public function validate_selection($value$message_identifier$error_message)
    {
        if (
    $value == '0')
        {
            
    $this->form_validation->set_message($message_identifier$error_message);
            return 
    false;
        }
        else
            return 
    true;

    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo