Re: Overloading Limitation- Can Someone Confirm?
| From: | Jochem Maas | Date: | Fri, 31 Mar 2006 08:35:31 +0000 |
| Subject: | Re: Overloading Limitation- Can Someone Confirm? | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-232985@lists.php.net to get a copy of this message | ||
Jasper Bryant-Greene wrote:
Jochem Maas wrote:a couple of things: I tested on 5.0.4 yesterday (should have mentioned that) - just tried on 5.1.1 and the behaviour there _seems_ to agree with your premise ... but not completely; the outcome is expected but the code doesn't do what I believe it should be doing with regard to guarding against arbitrary crap into an object via __set() - I assume people use __set() to protect variables, in the same way as 'normal' setters are often used. I modified you test code a little - notice how __set() is only called once (the second 'set' is actually a 'get'!): <?php class T {Jasper Bryant-Greene wrote:That's weird, because I did get around to testing it before I saw your mail, and in my test it works as *I* expect (PHP 5.1.2)...Jochem Maas wrote:you guess wrong :-) .. I couldn't resist testing it: php -r ' class T { private $var = array(); function __set($k, $v) { $this->var[$k] = $v; }I think its a misunderstanding on the one side and a limitation on the other, you can't use overloading directly on items of an overloaded array e.g:No, I'm pretty sure (too lazy and tired right now to test...) that ifecho $tc->arr['a']this is triggers a call to __get() with the $key parameter set to something like (I'm guessing) "arr['a']"function __get($k) { var_dump($k); }} $t = new T; $t->arr = array(); $t->arr["a"] = 1; echo "OUTPUT: \n"; var_dump($t->arr); var_dump($t->arr["a"]); var_dump($t); '
private $array = array();
public function __get( $key ) {
echo "Getting $key\n";
return $this->array[$key];
}
public function __set( $key, $value ) {
echo "Setting $key\n";
$this->array[$key] = $value;
}
}
$t = new T;
$t->insideArray = array();
$t->insideArray["test"] = "testing!";
var_dump( $t );
?>
OUTPUT:
Setting insideArray
Getting insideArray
object(T)#1 (1) {
["array:private"]=>
array(1) {
["insideArray"]=>
array(1) {
["test"]=>
string(8) "testing!"
}
}
I personally feel that the fact that $t->array['insideArray']['test'] is
being set (why does __set() seem to return a reference) is unwanted ...
it circumvents any variable checking logic I might have in __set()
how do like them apples? ;-)