Marco000 Posted March 15, 2009 Share Posted March 15, 2009 First of apologies for any inconvenience if this has been posted before as I’m unsure of what it could possibly be called/come under besides something like "reserved variables"? I have a class that pulls a result out of a database and loads them into class variables by the database field name like the following // Load the results into the class foreach ($array as $name => $value) { $this->$name = $value; } This works fine as expected but the problem occurs when I try to echo one of the fields names in the database (it’s called "string") It ends up being like echo $advert->string; and it’s not outputting anything? In Dreamweaver its showing the class variable "string" as a different colour so I’m guessing something isn’t right in terms of "string" being reserved or a function? How could I get the final/desired output/action I’m looking for here as I’m completely stumped! I normally Google for my solutions but have no idea what I have come across is called. PS. Sorry for anything I do wrong, just signed up and don’t know the ropes yet in terms of acceptable behaviour, community expectations and ways to post queries. Quote Link to comment https://forums.phpfreaks.com/topic/149536-echo-class-string-reservedfunction-names/ Share on other sites More sharing options...
Renlok Posted March 15, 2009 Share Posted March 15, 2009 well you could do it like $reserved = array('string'); // Load the results into the class foreach ($array as $name => $value) { if(in_array($name, $reserved)) $name = $name . '0'; $this->$name = $value; } Quote Link to comment https://forums.phpfreaks.com/topic/149536-echo-class-string-reservedfunction-names/#findComment-785342 Share on other sites More sharing options...
Marco000 Posted March 15, 2009 Author Share Posted March 15, 2009 But that means i might aswell change the field name in the database as it acomplishs the same thing? also is there a list of reserved variable names? to look over to increase my knowledge of this? I notice this happens with "int" too (echo $object->int) Quote Link to comment https://forums.phpfreaks.com/topic/149536-echo-class-string-reservedfunction-names/#findComment-785368 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.