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. 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. 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 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 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. 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
Archived
This topic is now archived and is closed to further replies.