|
Posted by Flibble on 11/21/91 11:24
"Janwillem Borleffs" <jw@jwscripts.com> wrote in
news:430795cb$0$67724$dbd4f001@news.euronet.nl:
> 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
>
>
>
Darn I'm sorry, I spent all day trying to work the problem out and
stuffed up when posting it :(
Anyway thank you for your time, and because of my stoopidness I don't
think the problem highlighted itself.
So I wrote this [tested] :) To try and highlight my problem. This is
extracted from the actual class causing the problem (took a while)
---------------------------------------------------------------
class test
{
private $itemArray;
private $writeProperties;
private $updatedFields;
function __construct()
{
$this->writeProperties = array( "Name", "Age" );
$this->updatedFields = array();
}
function __set( $property, $value )
{
$this->itemArray[$property] = $value;
$this->updatedFields[] = $property;
}
function __get( $property )
{
return $this->itemArray[$property];
}
public function setArray( $array )
{
foreach( $this->writeProperties as $property )
{
if( isset( $array[$property] ) && $array[$property] !=
"" )
{
$this->$property = $array[$property];
}
}
}
public function getUpdatedFields()
{
return $this->updatedFields;
}
}
$obj = new test();
$dataArray = array( "Name"=>"Me", "Age"=>"30" );
$obj->setArray( $dataArray );
echo "Fields updated -> <pre>";
print_r ($obj->getUpdatedFields() );
echo "<pre>";
---------------------------------------------------------------
If you look at the property $updatedFields it should end up populated
with two elements, "Name" & "Age". However they are populated with zero
values.
The curious thing is that inside the __set function the property has the
correct values in it, as soon as it moves outside the method they are
zero.....Can't see why it is doing this (might be code blind by now
though :( )
Apologise again for the bad initial post...
thanks
[Back to original message]
|