Q:

PHP program to print the class name using magic constant __CLASS__

belongs to collection: PHP Classes & Objects Programs

0

Here, we will print the name of the class using the magic constant __CLASS__, where it was used. The __CLASS__ constant returns case sensitive class 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 class name using magic constant __CLASS__  is given below. The given program is compiled and executed successfully.

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

Output:

Class Name: Sample

Explanation:

In the above program, we created a class Sample that contains a function MyFunction(). In the MyFunction(), we printed the name of the class using the __CLASS__ magic constant. After that, we created the object of Sample class and called the function MyFunction() that will print the Name of the class 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 print the method name with the asso... >>
<< PHP program to print the function name using magic...