Q:

PHP program to print the method name with the associated class name using magic constant __METHOD__

belongs to collection: PHP Classes & Objects Programs

0

Here, we will print the name of the method with the associated class name using magic constant __METHOD__, where it was declared. The __METHOD__ constant returns case sensitive method name.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

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

<?php
    //PHP program to print the method name with the 
    //associated class using magic constant __METHOD__.
    class Sample
    {
        public function MyMethod()
        {
            print(__METHOD__."<br>");
        }
    }
    
    $S=new Sample();
    $S->MyMethod();
?>

Output:

Sample::MyMethod

Explanation:

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP Classes & Objects Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
PHP program to demonstrate the use of printf() fun... >>
<< PHP program to print the class name using magic co...