Jasper Bryant-Greene wrote:
Jochem Maas wrote:
Jasper Bryant-Greene wrote:
Jochem Maas wrote:
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:
echo $tc->arr['a']
this is triggers a call to __get() with the $key parameter set to
something like
(I'm guessing) "arr['a']"
No, I'm pretty sure (too lazy and tired right now to test...) that if
you guess wrong :-) .. I couldn't resist testing it:
php -r '
class T { private $var = array();
function __set($k, $v) { $this->var[$k] = $v; }
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);
'
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)...
My comments earlier were based on the fact that it would not be good
to limit what can be put in an object through __get and __set
effectively to scalar variables, and I would expect the engine
developers to realise that. I think they have (unless I've done
something stupid in my test code...):
Code:
<?php
class T {
private $array = array();
public function __get( $key ) {
return $this->array[$key];
}
public function __set( $key, $value ) {
$this->array[$key] = $value;
}
}
$t = new T;
$t->insideArray = array();
$t->insideArray['test'] = 'testing!';
var_dump( $t );
?>
Output:
object(T)#1 (1) {
["array:private"]=>
array(1) {
["insideArray"]=>
array(1) {
["test"]=>
string(8) "testing!"
}
}
}
Dont know if you guys see the MAJOR difference between your code, so I
will point it out.
Jasper did this
function __get($k) {
var_dump($k);
}
Jochem did this
public function __get( $key ) {
return $this->array[$key];
}
First off, the required public before the function call was not
included, secondly, Jasper is var_dumping the key of the array, not the
array it self.
Hope this helps
And yes, it should work the way Jochem was expecting. At least that is
what the manual says, and my testing says.
But you have to make sure you include all the needed parts to make it
work correctly.
Thanks Jim
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php