Some Helpful Functions in PHP to get the Information About Class and Object

1. get_class: By using this, we can get the class name of an object.

Example 1

snippet
<?php
	class cls1
	{
		
	}
	$obj=new cls1();
	echo get_class($obj);
?>

Output:

Some Helpful Functions in PHP to get the Information About Class and Object

2. get_class_vars: It is used to get all variables of a class as Array elements.

Example 2

snippet
<?php
	class cls1
	{
		var $x=100;
		var $y=200;
	}
	print_r(get_class_vars("cls1"));
?>

Output:

Some Helpful Functions in PHP to get the Information About Class and Object

3. get_class_methods: To get the all methods of a class as an array.

Example 3

snippet
<?php
	class cls1
	{
		function fun1()
		{
		}
		function fun2()
		{
		}
	}
	print_r(get_class_methods("cls1"));
?>

Output:

Some Helpful Functions in PHP to get the Information About Class and Object

4. get_declare_classes: To get the all declare classes in current script along with predefined classes.

Example 4

snippet
<?php
	class cls1
	{
	
	}
	print_r(get_declared_classes());
?>

Output:

Some Helpful Functions in PHP to get the Information About Class and Object

5. get_object_vars: To get all variables of an object as an array.

Example 5

snippet
<?php
	class cls1
	{
		var $x=100;
		var $y=200;
	}
	$obj= new cls1();
	print_r(get_object_vars($obj));
?>

Output:

Some Helpful Functions in PHP to get the Information About Class and Object

6. class_exists: To check whether the specified class is existed or not.

Example 6

snippet
<?php
	class cls1
	{
		
	}
	echo class_exists("cls1");
?>

Output:

Some Helpful Functions in PHP to get the Information About Class and Object

7. is_subclass_of: By using this function we can check whether the 1st class is subclass of 2nd class or not.

Example 7

snippet
<?php
	class cls1
	{
		
	}
	class cls2 extends cls1
	{
	}
	echo is_subclass_of("cls2","cls1");
?>

Output:

Some Helpful Functions in PHP to get the Information About Class and Object

8. method_exists: By using this function we can check whether the class method is existed or not.

Example 8

snippet
<?php
	class cls1
	{
		function fun1()
		{
		}
	}
	echo method_exists("cls1","fun1");
?>

Output:

Some Helpful Functions in PHP to get the Information About Class and Object
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +