zoukankan      html  css  js  c++  java
  • PHP学习笔记二十六【类的重载】

    <?php
      //重载:
      //函数名一样,通过函数的参数个数或者是参数类型不同,达到调用同一个函数名
    
        Class A{
         // public function test1(){
          // echo "test1()";
         // }
         // public function test1($name)
         // {
            // echo "hello world";
         // }
          //PHP不支持以上的重载方式
          
           public function test1(){
          echo "调用test1()";
         }
         public function test2($name)
         {
            echo "调用test2()";
            echo "<br/>".__CLASS__."<br/>";//输出在当前哪个类中
                echo "<br/>".__FUNCTION__."<br/>";//输出函数名称
         }
          
          
          
          //使用魔术方法定义方法的重载
          function __call($method,$p)
          {
            if($method=="test")
            {
                if(count($p)==1)
                {
                  $this->test1($p);
                }else if(count($p)==2)
                {
                  $this->test2($p);
                }
            }
          }
        }
         $a=new A();
         $a->test("张三");
         $a->test("张三",34);
         
         
         echo "<br/>".__LINE__; //魔术常量输出当前行
         echo "<br/>".__FILE__; //输出文件路径
         echo "<br/>".__DIR__; //目录
         
      ?>
  • 相关阅读:
    第九章 表单效验
    第八章 使用jQuery操作DOM
    第七章 jQuery中的事件与动画
    CDQZ Day2
    HDU 3783
    CDQZ Day1
    BZOJ 2935/ Poi 1999 原始生物
    Luogu P1801 黑匣子_NOI导刊2010提高(06)
    Intelligent Poetry
    Great Expectations
  • 原文地址:https://www.cnblogs.com/sumg/p/4052850.html
Copyright © 2011-2022 走看看