Jump to content

Archived

This topic is now archived and is closed to further replies.

ober

best way to update class variables

Recommended Posts

I am making a new product display class and I have all kinds of settings that are seperate variables within the class.  Is there an easy way to update these values without creating a ton of small functions like this:
[code]
<?php
function update_headerbgcolor($color)
{
      this->headerbgcolor = $color;
}
?>[/code]

Any help appreciated.

Share this post


Link to post
Share on other sites
Nevermind... I'm an idiot.  I forgot I could do $objectname->varname = x outside of the class.

Share this post


Link to post
Share on other sites
And even if that weren't possible..

[code]<?php
function update_property($property, $value)
{
  $this->$property = $value;
}
?>[/code]

EDIT: Correction

Share this post


Link to post
Share on other sites
With PHP4 you can just set with
[code]
$obj->var = $value.[/code]

Same goes for PHP5 public variables

For PHP5 private vars you can define a "magic function"
[code]
function __set ($var , $value) {
    $this->$var = $value;
}[/code]

which will then allow
[code]$obj->privateVar = $value;[/code]

hth

Share this post


Link to post
Share on other sites
Eh... I'm stuck with PHP4 for this client.  I think I'll go with the first method, but I hadn't thought of passing the property and the value... the client will be updating these, so I'm trying to keep it as simple as possible.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.