Syntax error How to get current function name in PHP?

How to get current function name in PHP?



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"
Updated on: 2019-12-27T07:34:12+05:30

644 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements