__set() / __get() : limitation and/or unintentional behavoiur?

From: Date: Sat, 01 Apr 2006 00:49:10 +0000
Subject: __set() / __get() : limitation and/or unintentional behavoiur?
Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
I ran the following code on 5.0.4 and 5.1.0 with identical results...

Could someone shed light one whether the observed behaviour is
intentional and/or correct?

I expected that one of the following would occur (which obviously doesn't :-):

1. the line commented with 'SET 2' would trigger a
call to __set() (which should fail?).

2. the key "test" would be set in the array returned
by __get() but not it the relevant array stored in
$this->array["insideArray"].

The reason I question whether what si observed below is wanted behaviour is
because I thought the __set() functionality was there to be able
to protect/control what stuffed into an object ... but apparently it's
rather easy to side step.

tia,
Jochem

<?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(); // SET 1 $t->insideArray["test"] = "testing!"; // SET 2 var_dump( $t ); ?> OUTPUT: Setting insideArray Getting insideArray object(T)#1 (1) { ["array:private"]=> array(1) {
    ["insideArray"]=>
    array(1) {
      ["test"]=>
      string(8) "testing!"
    }
}

Thread (7 messages)

« previous php.internals (#22633) next »