PHP program to print the function name using magic constant __FUNCTION__

Here, we are going to learn how to print the function name using magic constant __FUNCTION__ in PHP?
Submitted by Nidhi, on November 18, 2020 [Last updated : March 13, 2023]

PHP Magic Constant | __FUNCTION__ Example

Here, we will print the name of the function using the magic constant __FUNCTION__, where it was used. The __FUNCTION__ constant returns case sensitive function name.

PHP code to demonstrate the example of magic constant __FUNCTION__

The source code to print the function name using magic constant __FUNCTION__  is given below. The given program is compiled and executed successfully.

<?php
    //PHP program to print the function name 
    //using magic constant __FUNCTION__.
    class Sample
    {
        public function MyFunction()
        {
            print("Function Name: ".__FUNCTION__."<br>");
        }
    }
    
    $S=new Sample();
    $S->MyFunction();
?>

Output

Function Name: MyFunction

Explanation

In the above program, we created a class Sample that contains a function MyFunction(). In the MyFunction(), we printed the name of the function using the __FUNCTION__ magic constant. After that, we created the object of Sample class and called the function MyFunction() that will print the Name of the function on the webpage.

PHP Class & Object Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.