dongweng9474 2014-09-13 18:08
浏览 51
已采纳

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

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;
                }
            }           
        }
  • 写回答

3条回答 默认 最新

  • dousu8767 2014-09-13 18:13
    关注

    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
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭