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

Php 如何在另一个方法中调用方法?,php,function,Php,Function,我有一个班级-验证课程 里面有一个构造函数 检查\u username函数,该函数包含两个验证用户名的其他方法。我想在调用check_username方法时调用这三个方法 代码: 函数检查\u用户名(){ //检查用户名是否为空 函数验证\空\用户名(){ 如果($this->empty_username){ echo“请填写用户名字段””; 返回false; } } //检查用户名长度是否短 函数验证\用户名\长度\最小值(){ 如果($this->get_username_

我有一个班级-验证课程

  • 里面有一个构造函数

  • 检查\u username函数,该函数包含两个验证用户名的其他方法。我想在调用check_username方法时调用这三个方法

代码:

函数检查\u用户名(){
//检查用户名是否为空
函数验证\空\用户名(){
如果($this->empty_username){
echo“
  • 请填写用户名字段”
  • ”; 返回false; } } //检查用户名长度是否短 函数验证\用户名\长度\最小值(){ 如果($this->get_username_length<3)和($this->empty_username!==true)){ echo“
  • 提供的用户名太短了!
  • ”; 返回false; } } //检查用户名长度是否过长 函数验证\用户名\长度\最大值(){ 如果($this->get\u username\u length>15){ echo“
  • 用户名太长”
  • ; 返回false; } } }
    您不是在函数中调用函数,而是定义它们。您应该在内部调用
    check_username()
    ,并可以在外部定义它们

    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
        }
    }
    
    类验证\u类
    {
    函数验证\用户名\长度\最小值(){
    如果($this->get_username_length<3)和($this->empty_username!==true)){
    echo“
  • 提供的用户名太短了!
  • ”; 返回false; } } //检查用户名长度是否过长 函数验证\用户名\长度\最大值(){ 如果($this->get\u username\u length>15){ echo“
  • 用户名太长”
  • ; 返回false; } } 函数验证\空\用户名(){ 如果($this->empty_username){ echo“
  • 请填写用户名字段”
  • ”; 返回false; } } 函数检查\u用户名(){ //检查用户名是否为空 $this->validate_empty_username($this->empty_username);//调用函数 //检查用户名长度是否短 $this->validate_username_length_min();//调用函数 //检查用户名长度是否过长 $this->validate_username_length_max();//调用函数 } }
    我不建议将子条件语句放在函数中

    我们只需做以下几点:

    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;
                    }
            }
    
    函数检查\u用户名(){
    //检查用户名是否为空
    如果($this->empty_username){
    echo“
  • 请填写用户名字段”
  • ”; 返回false; }elseif($this->get_username_length<3)和($this->empty_username!==true)){ echo“
  • 提供的用户名太短了!
  • ”; 返回false; }elseif($this->get\u username\u length>15){ echo“
  • 用户名太长”
  • ; 返回false; }否则{ 返回true; } }

    希望这有所帮助?

    您必须对类的隐藏方法使用私有声明。将所有内部函数都设置为私有方法。

    我认为您必须将
    check\u username
    更改为类。函数用于执行小任务,这就是它们不嵌套的原因。因此,不能在函数内部创建函数,因为它违反了函数定义
    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;
                    }
            }