Friday, December 12, 2025
HomeLanguagesPHP | ReflectionClass getConstructor() Function

PHP | ReflectionClass getConstructor() Function

The ReflectionClass::getConstructor() function is an inbuilt function in PHP which is used to return the constructor of the specified class or NULL if the class is not having any constructor.

Syntax:

ReflectionMethod ReflectionClass::getConstructor( void )

Parameters: This function does not accept any parameter.

Return Value: This function returns the constructor of the specified class or NULL if the class is not having any constructor.

Below programs illustrate the ReflectionClass::getConstructor() function in PHP:

Program 1:




<?php
  
// Using ReflectionClass over the class named as ReflectionClass
$Class = new ReflectionClass('ReflectionClass');
  
// Calling the getConstructor() function 
$constructor = $Class->getConstructor();
  
// Getting the constructor for the defined Class
var_dump($constructor);
?>


Output:

object(ReflectionMethod)#2 (2) {
  ["name"]=>
  string(11) "__construct"
  ["class"]=>
  string(15) "ReflectionClass"
}

Program 2:




    
// Defining a user-defined class Company
class Company {
    public function neveropen() { }
    static function gfg() { }
}
    
// Using ReflectionClass over the class Company
$A = new ReflectionClass("Company");
    
// Calling the getConstructor() function
$B = $A->getConstructor();
    
// Getting the constructor for the defined Class
// or NULL if the constructor is not present
var_dump($B);
?>


Output:

NULL

Reference: https://www.php.net/manual/en/reflectionclass.getconstructor.php

RELATED ARTICLES

Most Popular

Dominic
32445 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6814 POSTS0 COMMENTS
Nicole Veronica
11952 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12029 POSTS0 COMMENTS
Shaida Kate Naidoo
6949 POSTS0 COMMENTS
Ted Musemwa
7199 POSTS0 COMMENTS
Thapelo Manthata
6895 POSTS0 COMMENTS
Umr Jansen
6882 POSTS0 COMMENTS