bobthecow/psysh

sudo does not work with private constructor

tgr opened this issue · 2 comments

tgr commented
$ psysh 
Psy Shell v0.11.8 (PHP 7.4.30 — cli) by Justin Hileman
>>> class C {
... private function __construct() {}
... }
>>> sudo new C
PHP Error:  Call to private C::__construct() from invalid context in Psy Shell code on line 1

Handling this case would be messy but possible:

>>> class C {
...     private $a;
...     private $b;
...     private function __construct($a) {
...         $this->a = $a;
...         $this->b = 0;
...     }
... }
>>> $rc = new ReflectionClass(C::class)
>>> $c = $rc->newInstanceWithoutConstructor()
>>> $rcon = $rc->getConstructor()
>>> $rcon->setAccessible(true)
>>> $rcon->invoke($c, 1)

>>> dump -a $c
C {#2753
  -a: 1,
  -b: 0,
}

This is a somewhat fringe case, not sure it's worth the code complexity it would require, but it can be useful sometimes.

This is exactly the sort of shenanigans sudo was made for 🙂

Wanna open a PR?

👀👀

Screenshot 2022-11-05 at 8 50 48 AM