You are here: Re: Accessing properties via vars « PHP Language « IT news, forums, messages
Re: Accessing properties via vars

Posted by Janwillem Borleffs on 10/14/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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация