|
Posted by Janwillem Borleffs on 11/21/76 11:24
Dean L wrote:
> I am having some problems accessing object properties through the use
> of a variable. I am using overloading in php 5.0.4 to create an
> array holding the property values.
>
> test snippet (un tested)
>
[...]
Curious, you are posting a question about an untested snippet...
> Now normally I would expect property 'Name' to be set to 'Me', and
> whilst the script is executing inside the __set method it does.
> However as soon as __set returns back to the 'test' method the name
> element of $itemArray is zero'd off :( It's like the scope of the
> value is being lost...
>
Use $this->itemArray instead of $itemArray inside the methods, as in:
function __set($property, $value) {
$this->itemArray[$property] = $value;
}
Tested snippet:
class example {
private $itemArray;
function __set($property, $value) {
$this->itemArray[$property] = $value;
}
function __get($property) {
return $this->itemArray[$property];
}
function test($array) {
foreach ($array as $k => $v) {
$this->$k = $v;
}
}
}
Consider a more descriptive name for the test function, like setArray or
define a __call method instead.
JW
Navigation:
[Reply to this message]
|