Computer >> Computer tutorials >  >> Programming >> PHP

How to get current function name in PHP?


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

Example

<?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 −

<?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"