Page 1 of 1

Class property access through variable

Posted: Mon Jan 02, 2006 8:31 am
by cpapaiz
i have the following class:

Code: Select all

class simple{
    $property1=3
}
i want to change the value of the property with an external variable with the name of the property on it.
it would be something like this i suppose:

Code: Select all

$obj=new simple;

$var='property1';
$value = 7;

$obj->$var=$value;
but it doesnt work and i dont know what to do....

Thanks in advance..


Posted: Mon Jan 02, 2006 8:59 am
by Chris Corbyn
I'm not sure what you mean...

There are a few possibilities I think you could mean. If you don't want to directly manipulate the variable it's sensible to use getters and setters:

Code: Select all

class myClass
{
    var $property = 0;

    function setProperty($value)
    {
        $this->property = $value;
    }

    function getProperty()
    {
        return $this->property;
    }
}

$obj = new myClass();
echo $obj->getProperty(); // 0

$obj->setProperty(12);
echo $obj->getProperty(); // 12

$obj->setProperty('foo');
echo $obj->getProperty(); // foo
It sounds like you're referring more to "references" however. References are basically pointers to variables in memory (a shortcut, or path if you like).

Code: Select all

class myClass
{
    var $property = 0;

    function setProperty($value)
    {
        $this->property = $value;
    }

    function getProperty()
    {
        return $this->property;
    }
}

$obj = new myClass();
echo $obj->getProperty(); // 0

$global_value = &$obj->property; //Create a reference (pointer)

echo $global_value; // 0

$global_value = 7;

echo $global_value; // 7

echo $obj->getProperty(); // 7

Posted: Mon Jan 02, 2006 9:12 am
by cpapaiz
thanks d11wtq but that is not exacly what im trying...
let me try to explain it better:
i have a class with a lot of properties but in different situations i need to set and get different properties of this class.
the properties needed to be changed are in an array named $config like this:

Code: Select all

array( propertyname1 => propertyvalue1,
           propertyname2 => propertyvalue2,
         )
so i want to use something like this:

Code: Select all

foreach ($config as $key => $value){
    $cbx->$key=$value;
}
to set the properties i need.
but it doesnt work and i cant figure out how to.....

PS: i dont have access to this class source...it is a dll

thanks again...

Posted: Mon Jan 02, 2006 9:24 am
by Chris Corbyn
Setting dynamic properties on-the-fly like that should still work.

Code: Select all

class myClass 
{
    var $foo = 0, $bar = 0;
    
    function addSomeNewProperties($array)
    {
        foreach ($array as $key => $value)
        {
            $this->$key = $value;
        }
    }
}

$obj = new myClass();

print_r($obj);
/*
  myClass Object
(
    [foo] => 0
    [bar] => 0
)
*/

$array = array ( 'one' => 1, 'two' => 2 );

$obj->addSomeNewProperties($array);

print_r($obj);

/*
 myClass Object
(
    [foo] => 0
    [bar] => 0
    [one] => 1
    [two] => 2
)
*/
:)

EDIT | Is this PHP4 or PHP5 ? If the properties already exist in a PHP5 class they may be defined as private and so you can't fiddle with them like that.