I am wondering if there is any way to implement some sort of "reverse visibility" in PHP? I understand that children can access public and protected members of a parent class, like this:
class A {
protected function __construct() { echo 'yes'; }
}
class B extends A {
public function __construct() { parent::__construct(); }
}
$b = new B; // yes
$a = new A; // fatal error
What I am hoping for is the opposite. I would like some way of making A able to call B's constructor, but B unable to call A's, without A being a subclass of B. Ideally, neither of them would be subclasses -- they would be two distinct classes.
I know that some languages have something called "nested classes" which can accomplish this. Is there any way to trick PHP into doing it, since it doesn't support nested classes?
I have read through this SO question: https://stackoverflow.com/questions/16424257/nested-or-inner-class-in-php but it all sounds a bit to "hack-y" to me. It's also three years old. Perhaps PHP 7 has made some improvements that might make this easier?