Re: Overloading Limitation- Can Someone Confirm?

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

 



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