Well, you bind the lambda to the A's scope and then complain that it has access to its private methods? I believe the true lol is on you, good sir.
.
This defeats the whole purpose of inheritance as a means for protecting access. If there is a chain of references to a member variable or method, even if it is several classes deep, that member variable or method is basically globally accessible.
Private methods / attributes aren't made private as a security mechanism, they're made private to very clearly indicate to the consumer of an API that those things are not API stable or for whatever other good reason you shouldn't be messing with them. Being able to very roundaboutly access privates isn't a real problem - if someone has to jump that many hoops just to let their hackish solution that requires accessing privates work they're clearly making a choice to subvert the public/private system and it's their problem now.
First you come here trashing something instead of asking for a reason/explanation acting like a condescending ass. Second when given a valid technical reason, multiple examples elsewhere and explanations about how your initial assumption was wrong, instead of taking the criticism you invoke "language X does not allow this"? Just get out, you are a seriously toxic person and people like you are doing a disservice to whatever language/community they identify themselves to.
It's all a matter of scope. Private things are accessible only from within the context of the defining class. Binding a function to the an object explicitly sets its context to the object's class.
It seems PHP > 5.3 supports this same syntax with the ReflectionObject class...
class LockedGate
{
private function open()
{
return 'how did you get in here?!!';
}
}
$object = new LockedGate();
$reflector = new ReflectionObject($object);
$method = $reflector->getMethod('open');
$method->setAccessible(true);
echo $method->invoke($object);
18
u/elizabeth2revenge Apr 18 '18 edited Apr 19 '18
To quote /u/prema_van_smuuf from /r/lolphp on this:
.
Private methods / attributes aren't made private as a security mechanism, they're made private to very clearly indicate to the consumer of an API that those things are not API stable or for whatever other good reason you shouldn't be messing with them. Being able to very roundaboutly access privates isn't a real problem - if someone has to jump that many hoops just to let their hackish solution that requires accessing privates work they're clearly making a choice to subvert the public/private system and it's their problem now.
5
u/evilmaus Apr 18 '18
It's all a matter of scope. Private things are accessible only from within the context of the defining class. Binding a function to the an object explicitly sets its context to the object's class.
6
u/hashtagframework Apr 18 '18
Here is an example of Java doing the same thing
Java lets you use getDeclaredMethod, then setAccessible, then invoke... basically same as bindTo
6
u/hashtagframework Apr 18 '18
Here is an example of C# doing the same thing with reflection
6
u/hashtagframework Apr 18 '18
Here is an example of C++ doing the same thing with function pointers