How to get current function name in PHP?

PHPServer Side ProgrammingProgramming

To get the current function name in PHP, the code is as follows−

Example

 Live Demo

<?php
   class Base {
      function display() {
         echo "\nBase class function declared final!";
         var_dump(__FUNCTION__);
      }
      function demo() {
         echo "\nBase class function!";
      }
   }
   class Derived extends Base {
      function demo() {
         echo "\nDerived class function!";
      }
   }
   $ob = new Base;
   $ob->demo();
   $ob->display();
   $ob2 = new Derived;
   $ob2->demo();
   $ob2->display();
?>

Output

This will produce the following output−

Base class function!
Base class function declared final!string(7) "display"
Derived class function!
Base class function declared final!string(7) "display"

Example

Let us now see another example −

 Live Demo

<?php
   class Base {
      function display() {
         echo "\nBase class function declared final!";
         var_dump(__FUNCTION__);
      }
      function demo() {
         echo "\nBase class function!";
         var_dump(__METHOD__);
      }
   }
   class Derived extends Base {
      function demo() {
         echo "\nDerived class function!";
      }
   }
   $ob = new Base;
   $ob->demo();
   $ob->display();
   $ob2 = new Derived;
   $ob2->demo();
   $ob2->display();
?>

Output

This will produce the following output−

Base class function!string(10) "Base::demo"
Base class function declared final!string(7) "display"
Derived class function!
Base class function declared final!string(7) "display"
raja
Published on 27-Dec-2019 12:04:12
Advertisements