note 126122 added to reflectionclass.newinstance

From: Date: Sun, 30 May 2021 00:23:32 +0000
Subject: note 126122 added to reflectionclass.newinstance
Groups: php.notes 
Request: Send a blank email to php-notes+get-216446@lists.php.net to get a copy of this message
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 ---- Server IP: 45.112.84.4 Probable Submitter: 45.112.85.242 (proxied: 36.68.55.69) ---- Manual Page -- https://php.net/manual/en/reflectionclass.newinstance.php Edit -- https://main.php.net/note/edit/126122 Del: integrated -- https://main.php.net/note/delete/126122/integrated Del: useless -- https://main.php.net/note/delete/126122/useless Del: bad code -- https://main.php.net/note/delete/126122/bad+code Del: spam -- https://main.php.net/note/delete/126122/spam Del: non-english -- https://main.php.net/note/delete/126122/non-english Del: in docs -- https://main.php.net/note/delete/126122/in+docs Del: other reasons-- https://main.php.net/note/delete/126122 Reject -- https://main.php.net/note/reject/126122 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#216446) next »