Re: Overloading Limitation- Can Someone Confirm?
| From: | Jochem Maas | Date: | Sat, 01 Apr 2006 07:50:42 +0000 |
| Subject: | Re: Overloading Limitation- Can Someone Confirm? | ||
| References: | 1 2 3 4 5 6 7 8 9 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-233042@lists.php.net to get a copy of this message | ||
Chr is wrote:
You're right, it's not going through the __Set() method a second time. If you REALLY want to get confused by overloading behavior try the following code using your T class: $t = new T; $t->insideArray = array('a' => 'A', 'b' => 'B', 'c' => 'C'); foreach ($t->insideArray as $k => $v) {I can't tell you because I don't have access to a 5.1.2 CLI at the moment. I can tell you that this leads to a SegFault on 5.0.4, here is the output: Setting insideArray Getting insideArray Getting insideArray Getting insideArray Getting insideArray Getting insideArray Segmentation fault and on 5.1.1 it's not much better, the output is: Setting insideArray Getting insideArray Getting insideArray Getting insideArray Getting insideArray Getting insideArray Getting insideArray FATAL: emalloc(): Unable to allocate 1916888421 bytes$t->insideArray[$k] = null;} if (count($t->insideArray) > 0) {foreach ($t->insideArray as $k => $v) { echo $v; }} var_dump($t); Why does count() return > 0? Why has $t->insideArray become NULL instead of an empty array?
Chris chris@dented-planet.net On Mar 31, 2006, at 7:39 PM, Jochem Maas wrote:could you both take a look at this (I posted it already btw) - notice that the second 'set' action ($t->insideArray["test"] = "testing!";) is not going via __set() at all, it uses __get() BUT/AND then an item is set in the returned array ... and also in the [sub] array stored inside the object (even though nothing is being returned by reference): (I feel that this is not right somehow!) <?php class 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!" }} Jasper Bryant-Greene wrote:Jim Lucas wrote:Jasper Bryant-Greene wrote:[snip]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; }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.