Reflections
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 aReflectionClassinstance 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
Get singleton instance
Detect if method exists in class
Get class instance
Get all constants of the class
Get the name of a constant by its value
Get the value of a class property by its name
Creates an object and sets its properties in a case-insensitive manner.
Converts multiple arguments into an associative array.
Details
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.