What are some potential pitfalls of using eval in PHP, and are there alternative solutions for dynamically instantiating classes with unknown constructor parameters?

Using eval in PHP can be dangerous as it allows for arbitrary code execution and can introduce security vulnerabilities if not handled properly. Instead of using eval to dynamically instantiate classes with unknown constructor parameters, a safer alternative is to use reflection to inspect the class and its constructor parameters.

// Using reflection to dynamically instantiate classes with unknown constructor parameters
function instantiateClass($className, $constructorParams) {
    $reflectionClass = new ReflectionClass($className);
    $instance = $reflectionClass->newInstanceArgs($constructorParams);
    return $instance;
}

// Example usage
$className = 'ExampleClass';
$constructorParams = ['param1', 'param2'];
$instance = instantiateClass($className, $constructorParams);