Why can you call a private method on a new instance made inside a public method of the same class type?
class Foo
{
private function thePrivateMethod()
{
echo 'can not be called publicly?';
}
public function thePublicMethod()
{
$clone = new Foo;
$clone->thePrivateMethod();
}
}
$foo = new Foo();
$foo->thePublicMethod();
$foo->thePrivateMethod();
The above results in the following output when run in PHP 7.3.18
can not be called publicly?
Fatal error: Uncaught Error: Call to private method Foo::thePrivateMethod() from context
Intuitively, I would expect the first call to Foo::thePrivateMethod()
to also cause a fatal error. But I’m not able to find in the documentation that this behaviour would be allowed?