How to get the class name in PHP - onlyxcodes

Tuesday 14 May 2024

How to get the class name in PHP

In this tutorial, I will explain how to get the class name in PHP. When working with object-oriented PHP, it's common to face conditions where we need to retrieve the name of a class dynamically. Whether we are debugging, logging, or implementing certain design patterns, sometimes we need to how to fetch the class name is useful.


I'll go over some of the simple techniques that PHP provides to bring class names. Below, let's explore a few of them.


how to get the class name in php

1. Using get_class() Function:

PHP has a built-in function called get_class() that can be used to get the class name of an object.


I defined a function named getName() inside the User class, and I used the 'this' keyword argument to return the get_class() function.


When the "this" keyword is sent as an argument to this function, the class name will be returned. I invoked the getName() method using the User class object that I had generated, $obj.


<?php

class User 
{
   
	public function getName() 
	{
		echo get_class($this);
	}
}

$obj = new User();
$obj->getName(); 

?>

Output:


User

2. Using the get_called_class() Function

PHP has a built-in function called get_called_class() that can be used to find the class name where the static method is called.


In this example, I declared a static method called fetchClassName() from which the get_called_class() function is received.


Next, I called the static fetchClassName() function using the class name, double colon (::), and method name. 


Here, I've shown an example of a static method that you can call straight away without first creating a class object.


<?php

// make a class
class Employee 
{
   
	// declare a static function that returns the name of the class which calls it
	
	public static function fetchClassName() 
	{
		return get_called_class();
	}
}

//call the static function by using the class name and double colon (::)

echo Employee::fetchClassName(); 

?>

Output:


Employee

3. Using the ReflectionClass class

The built-in PHP class called ReflectionClass enables you to view a class's methods and properties while it's being used. It offers a mechanism for dynamically analyzing a class's methods and structure.


In this example, I have set the Reflection class to $this and used the Reflection class's getShortName() method to retrieve the class name.


<?php

//declare a class
class Student 
{   
	//make a function that returns the class name via reflection
       
	public function getClassName() 
	{
		return (new \ReflectionClass($this))->getShortName();   
	}
}
 
//create an object of a class with the new keyword 
   
$obj = new Student();

   
//retrieve the class name
   
echo $obj->getClassName();

?>

Output:


Student

4. Using the ::class keyword

The fully qualified name of a class can also be obtained by using the class keyword. 


In this example, I first constructed a class object, then I used the ::class keyword to apply the object to get the class name.


<?php

// create the class

class Employee {
   
}
   
//create a new object
   
$obj = new Employee();


//retrieve the class name from the object through ::class
   
echo $obj::class;

?>

Output:


Employee

We can also use ::class to get a class name directly without first creating a class object.


For example,


In this example, I used Employee::class, which generates no objects and returns the fully qualified Employee class name.


<?php

// create the class
   
class Employee {
   
}
   
// retrieve the class name through Employee::class without creating an object
   
echo Employee::class;

?>

Output:


Employee

No comments:

Post a Comment

Post Bottom Ad