|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2013-07-29 04:39 UTC] seyferseed at mail dot ru
Description: ------------ Method Closure::bind() can access private property of object. And modify it by reference. It is a violation of encapsulation. See script for example. http://3v4l.org/JE0eX Test script: --------------- <?php class Foo { private $bar = 'baz'; } $reader = function & ($object, $property) { $value = & Closure::bind(function & () use ($property) { return $this->$property; }, $object, $object)->__invoke(); return $value; }; $foo = new Foo(); $bar = & $reader($foo, 'bar'); $bar = 'tab'; var_dump($foo); Expected result: ---------------- Fatal error: Cannot access private property Foo::$bar in /in/JE0eX on line 8 Actual result: -------------- Output for 5.4.0 - 5.5.1 object(Foo)#2 (1) { ["bar":"Foo":private]=> &string(3) "tab" } PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Nov 04 18:00:01 2025 UTC |
But if i'm add this function to class Foo by code it in class, i can't access private property! And this is right. You say, that my Getter function used in Closure::bind() can change (like Setter) private property and this is right? I don't think so. Example: class Foo { private $bar = 'baz'; public function &getBar() { return $this->bar; } } $foo = new Foo(); $bar = $foo->getBar(); $bar = "tab"; echo $foo->getBar(); It's still "bar". And i don't know how i can change Private property of class by reference in this case. It will be wrong, if i can. And now i'm take my getter public function &getBar() { return $this->bar; } Put it in Closure::bind() and i can change Private property. This is really wrong. If i can change it only in function, that binded to Closure::bind(), like $value = & Closure::bind(function & () use ($property) { $this->property = "tab"; return $this->$property; }, $object, $object)->__invoke(); It's okay, becouse it statically inside Foo. But Client code can change Private property by reference! Is that right?