note 126122 rejected from reflectionclass.newinstance by cmb

From: Date: Sun, 30 May 2021 09:49:36 +0000
Subject: note 126122 rejected from reflectionclass.newinstance by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-216450@lists.php.net to get a copy of this message
Note Submitter: feryardiant at pm dot me ---- I don't know why it's not documented anywhere, when you (accidentally) pass an abstract class name you'll get the instance of Error class instead of ReflectionException class. <?php function reflect(string $class) { try { (new ReflectionClass($class))->newInstance(); } catch (Throwable $e) { echo get_class($e) . ' : ' . $e->getMessage() . PHP_EOL; } } abstract class A {} class B { private function __construct() {} } reflect(A::class); // => Error : Cannot instantiate abstract class A reflect(B::class); // => ReflectionException : Access to non-public constructor of class B

« previous php.notes (#216450) next »