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 "
Base class function declared final!";          var_dump(__FUNCTION__);       }       function demo() {          echo "
Base class function!";       }    }    class Derived extends Base {       function demo() {          echo "
Derived 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 "
Base class function declared final!";          var_dump(__FUNCTION__);       }       function demo() {          echo "
Base class function!";          var_dump(__METHOD__);       }    }    class Derived extends Base {       function demo() {          echo "
Derived 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
Updated on 27-Dec-2019 07:34:12

Advertisements