Re: Overloading Limitation- Can Someone Confirm?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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) {
    $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?

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



Chris
chris@xxxxxxxxxxxxxxxxx



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:

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);
'


[snip]

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);
}

Uhm, no I didn't. Jochem did :)

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.





--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux