ober Posted September 28, 2006 Share Posted September 28, 2006 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]<?phpfunction update_headerbgcolor($color){ this->headerbgcolor = $color;}?>[/code]Any help appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/22404-best-way-to-update-class-variables/ Share on other sites More sharing options...
ober Posted September 28, 2006 Author Share Posted September 28, 2006 Nevermind... I'm an idiot. I forgot I could do $objectname->varname = x outside of the class. Quote Link to comment https://forums.phpfreaks.com/topic/22404-best-way-to-update-class-variables/#findComment-100441 Share on other sites More sharing options...
acdx Posted September 28, 2006 Share Posted September 28, 2006 And even if that weren't possible..[code]<?phpfunction update_property($property, $value){ $this->$property = $value;}?>[/code]EDIT: Correction Quote Link to comment https://forums.phpfreaks.com/topic/22404-best-way-to-update-class-variables/#findComment-100451 Share on other sites More sharing options...
Barand Posted September 28, 2006 Share Posted September 28, 2006 With PHP4 you can just set with[code]$obj->var = $value.[/code]Same goes for PHP5 public variablesFor 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 Quote Link to comment https://forums.phpfreaks.com/topic/22404-best-way-to-update-class-variables/#findComment-100458 Share on other sites More sharing options...
ober Posted September 28, 2006 Author Share Posted September 28, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/22404-best-way-to-update-class-variables/#findComment-100461 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.