Re: Overloading Limitation- Can Someone Confirm?

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

 



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.

--
Jasper Bryant-Greene
General Manager
Album Limited

http://www.album.co.nz/     0800 4 ALBUM
jasper@xxxxxxxxxxx          021 708 334

--
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