Re: Overloading Limitation- Can Someone Confirm?
| From: | Jasper Bryant-Greene | Date: | Fri, 31 Mar 2006 23:16:09 +0000 |
| Subject: | Re: Overloading Limitation- Can Someone Confirm? | ||
| References: | 1 2 3 4 5 6 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-233022@lists.php.net to get a copy of this message | ||
Jim Lucas wrote:
Jasper Bryant-Greene wrote:Jochem Maas wrote: [snip]
you guess wrong :-) .. I couldn't resist testing it: php -r ' class T { private $var = array(); function __set($k, $v) { $this->var[$k] = $v; }[snip]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); '
Uhm, no I didn't. Jochem did :)Code: <?php class T {Dont know if you guys see the MAJOR difference between your code, so I will point it out. Jasper did thisprivate $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!" }} }function __get($k) {var_dump($k); }
Jochem did this public function __get( $key ) { return $this->array[$key]; }No, I did that.
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.Public is not required. I always put it regardless, but if you leave it off then PHP defaults to public for compatibility reasons. Jochem's code, which behaves incorrectly, does var_dump. Mine just returns the array key as you would expect. That's why Jochem's doesn't behave correctly with arrays. -- Jasper Bryant-Greene General Manager Album Limited http://www.album.co.nz/ 0800 4 ALBUM jasper@album.co.nz 021 708 334