class Reflections (View source)

The GenericDatabase\Helpers\Reflections class provides various reflection-based methods to interact with classes and objects in PHP.

It allows you to perform tasks such as getting class instances, retrieving constants and properties, creating objects, and setting properties in a case-insensitive manner.

Example Usage:

// Get a singleton instance of a class
$instance = Reflections::getSingletonInstance(MyClass::class);

// Check if a method exists in a class
$exists = Reflections::isSingletonMethodExists(MyClass::class);

// Get a class instance
$classInstance = Reflections::getClassInstance(MyClass::class);

// Get all constants of a class
$constants = Reflections::getClassConstants(MyClass::class);

// Get the name of a constant by its value
$constantName = Reflections::getClassConstantName(MyClass::class, $constantValue);

// Get the value of a class property by its name
$propertyValue = Reflections::getClassPropertyName(MyClass::class, 'propertyName');

// Create an object and set properties in a case-insensitive manner
$object = Reflections::createObjectAndSetPropertiesCaseInsensitive(MyClass::class, $constructorArgs, $propertyList);

// Convert multiple arguments into an associative array
$argsArray = Reflections::argsToArray($arg1, $arg2, $arg3);

Main functionalities:

  • Get the singleton instance of a class by calling a static method
  • Check if a method exists and is static in a class
  • Get a ReflectionClass instance for a class
  • Get all constants defined in a class
  • Get the name of a constant by its value
  • Get the value of a class property by its name
  • Create objects and set properties in a case-insensitive manner
  • Convert multiple arguments into an associative array

Methods:

  • getSingletonInstance($class): Retrieves the singleton instance of a class by calling a static method. Throws an exception if the method does not exist.
  • isSingletonMethodExists($class): Checks if a method exists and is static in a class. Throws an exception if the method does not exist or is not static.
  • getClassInstance($class): Returns a ReflectionClass instance for the given class.
  • getClassConstants($class): Returns an array of all constants defined in the class.
  • getClassConstantName($class, $field): Returns the name of a constant by its value.
  • getClassPropertyName($class, $prop): Returns the value of a class property by its name.
  • createObjectAndSetPropertiesCaseInsensitive($classOrObject, $constructorArgArray, $propertyList): Creates an object and sets its properties in a case-insensitive manner.
  • argsToArray(...$args): Converts multiple arguments into an associative array.

Fields:

  • defaultMethod: A static field that stores the default method name (getInstance) used for getting singleton instances.

Properties

static string $defaultMethod

Set default method

Methods

static object|false
getSingletonInstance(mixed $class)

Get singleton instance

static bool
isSingletonMethodExists(mixed $class)

Detect if method exists in class

static ReflectionClass
getClassInstance(mixed $class)

Get class instance

static array
getClassConstants(mixed $class)

Get all constants of the class

static string|int|bool
getClassConstantName(mixed $class, mixed $field)

Get the name of a constant by its value

static mixed
getClassPropertyName(mixed $class, mixed $prop)

Get the value of a class property by its name

static mixed
createObjectAndSetPropertiesCaseInsensitive(mixed $classOrObject, array $constructorArgArray, array $propertyList)

Creates an object and sets its properties in a case-insensitive manner.

static array
argsToArray(mixed ...$args)

Converts multiple arguments into an associative array.

Details

static object|false getSingletonInstance(mixed $class)

Get singleton instance

Parameters

mixed $class

The class object or instance

Return Value

object|false

The singleton instance of the class

Exceptions

Exceptions

static bool isSingletonMethodExists(mixed $class)

Detect if method exists in class

Parameters

mixed $class

The class object or instance

Return Value

bool

A boolean indicating if the method exists and is static

Exceptions

ReflectionException

static ReflectionClass getClassInstance(mixed $class)

Get class instance

Parameters

mixed $class

The class object or instance

Return Value

ReflectionClass

A ReflectionClass instance for the given class

Exceptions

ReflectionException

static array getClassConstants(mixed $class)

Get all constants of the class

Parameters

mixed $class

The class object or instance

Return Value

array

An array of all constants defined in the class

Exceptions

ReflectionException

static string|int|bool getClassConstantName(mixed $class, mixed $field)

Get the name of a constant by its value

Parameters

mixed $class

The class object or instance

mixed $field

The constant name or value

Return Value

string|int|bool

The name of a constant by its value

Exceptions

ReflectionException

static mixed getClassPropertyName(mixed $class, mixed $prop)

Get the value of a class property by its name

Parameters

mixed $class

The class object or instance

mixed $prop

The property name

Return Value

mixed

The value of a class property by its name

Exceptions

ReflectionException

static mixed createObjectAndSetPropertiesCaseInsensitive(mixed $classOrObject, array $constructorArgArray, array $propertyList)

Creates an object and sets its properties in a case-insensitive manner.

Parameters

mixed $classOrObject

The class object or instance, or the class name as a string.

array $constructorArgArray

An array of constructor arguments.

array $propertyList

An array of properties to be set on the object.

Return Value

mixed

The created object with the properties set in a case-insensitive manner.

Exceptions

ReflectionException

static array argsToArray(mixed ...$args)

Converts multiple arguments into an associative array.

Parameters

mixed ...$args

The arguments to convert.

Return Value

array

The converted associative array.