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__; //目录

     

  ?>

 

你可能感兴趣的:(学习笔记)