如何在另一个方法中调用方法?

I've a class - validate_class.

  • A Constructor inside.

  • check_username function that contains couple other methods that validate username. I want to call these three methods that are inside check_username method when it's called.

Code:

function check_username(){
            //Checks if is username empty
            function validate_empty_username(){
                if($this->empty_username){
                    echo "<li>Please fill username field</li>";
                    return false;
                }
            }       

            //Check username length is short
            function validate_username_length_min(){
                if(($this->get_username_length < 3) and ($this->empty_username !== true) ){
                    echo "<li>Username provided's too short!</li>";
                    return false;
                }
            }

            //Check username length is long
            function validate_username_length_max(){
                if($this->get_username_length > 15){
                    echo "<li>Username's too long</li>";
                    return false;
                }
            }           
        }

You are not calling functions inside function but defining them. You should call inside check_username() and you can define them outside it.

Class validate_class
{
    function validate_username_length_min(){
        if(($this->get_username_length < 3) and ($this->empty_username !== true) ){
            echo "<li>Username provided's too short!</li>";
            return false;
        }
    }

    //Check username length is long
    function validate_username_length_max(){
        if($this->get_username_length > 15){
            echo "<li>Username's too long</li>";
            return false;
        }
    }
    function validate_empty_username(){
         if($this->empty_username){
              echo "<li>Please fill username field</li>";
              return false;
             }
    } 
    function check_username(){
         //Checks if is username empty
         $this->validate_empty_username($this->empty_username);//calling function
         //Check username length is short
         $this->validate_username_length_min();//calling function
         //Check username length is long
         $this->validate_username_length_max();//calling function
    }
}

I wouldn't suggest putting the child conditional statements within functions.

Simply have as follows:

function check_username(){
            //Checks if is username empty
                if($this->empty_username){
                    echo "<li>Please fill username field</li>";
                    return false;
                } elseif(($this->get_username_length < 3) and ($this->empty_username !== true) ){
                    echo "<li>Username provided's too short!</li>";
                    return false;
                } elseif($this->get_username_length > 15){
                    echo "<li>Username's too long</li>";
                    return false;
                } else {
                    return true;
                }
        }

Hope this helps?

You must use private declaration for hided methods of your class. Make all inner functions as private methods.