PHP program to print the class name using magic constant __CLASS__

Here, we are going to learn how to print the class name using magic constant __CLASS__ in PHP?
Submitted by Nidhi, on November 18, 2020

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.

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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.