ttmt_101 Posted July 11, 2013 Share Posted July 11, 2013 Hi all This is following on from my previous questions. I'm creating an <select> menu from an array and adding it to a variable I'm also creating an Object. How can I add the select menu variable to the Object. This is all because I just need a named container to hold the select menu. <?php class Color{ var $select; } $col_Arr = array('Red','Green','Blue'); $select_menu = "<select>"; for($i=0; $i<count($col_Arr); $i++){ $select_menu .="<option>".$col_Arr[$i]."</option>"; } echo "</select>"; $My_Col = new Color; //How do I add $select_menu to $My_Col $My_Col->$select = $select_menu; $My_Col->$select_menu; ?> Quote Link to comment https://forums.phpfreaks.com/topic/280061-add-variable-to-an-object/ Share on other sites More sharing options...
web_craftsman Posted July 11, 2013 Share Posted July 11, 2013 var $select; Are you a fun of php4 ? Use public instead of var Do this: $My_Col->select = $select_menu; Quote Link to comment https://forums.phpfreaks.com/topic/280061-add-variable-to-an-object/#findComment-1440291 Share on other sites More sharing options...
cpd Posted July 11, 2013 Share Posted July 11, 2013 (edited) A select menu isn't a colour and shouldn't be a property of a "Color" object. A "Select" could be an object of its own. @web_craftsman: You shouldn't really declare properties on the fly as you then open a can of encapsulation issue worms. Edited July 11, 2013 by cpd Quote Link to comment https://forums.phpfreaks.com/topic/280061-add-variable-to-an-object/#findComment-1440292 Share on other sites More sharing options...
ttmt_101 Posted July 11, 2013 Author Share Posted July 11, 2013 @web_craftsman: what encapsulation issues? Quote Link to comment https://forums.phpfreaks.com/topic/280061-add-variable-to-an-object/#findComment-1440293 Share on other sites More sharing options...
ttmt_101 Posted July 11, 2013 Author Share Posted July 11, 2013 (edited) @web_craftsman: I'm trying to create a named container I can reference later in the code to hold a select menu. All this object will do is hold a reference to the select menu. Is there another way of doing this? Edited July 11, 2013 by ttmt_101 Quote Link to comment https://forums.phpfreaks.com/topic/280061-add-variable-to-an-object/#findComment-1440294 Share on other sites More sharing options...
web_craftsman Posted July 11, 2013 Share Posted July 11, 2013 your $select_menu variable is this named container. Quote Link to comment https://forums.phpfreaks.com/topic/280061-add-variable-to-an-object/#findComment-1440295 Share on other sites More sharing options...
cpd Posted July 11, 2013 Share Posted July 11, 2013 Use a variable. Why are you using an object? There is absolutely no need for an object here and this is not object oriented programming in the slightest. You're just creating objects and giving them properties they shouldn't have and then assigning something to the property; that entire process is basically the same as assigning a normal variable. Quote Link to comment https://forums.phpfreaks.com/topic/280061-add-variable-to-an-object/#findComment-1440296 Share on other sites More sharing options...
ttmt_101 Posted July 11, 2013 Author Share Posted July 11, 2013 cpd - I know this isn't OOP in the slightest but I needed a named container, I thought i was going to include other things in it. I think now i only need the select menu so it would work in a variable - I've made this so complicated for myself. Quote Link to comment https://forums.phpfreaks.com/topic/280061-add-variable-to-an-object/#findComment-1440298 Share on other sites More sharing options...
web_craftsman Posted July 11, 2013 Share Posted July 11, 2013 I thought i was going to include other things in it. For this you may use StdClass: Anonymous Objects Quote Link to comment https://forums.phpfreaks.com/topic/280061-add-variable-to-an-object/#findComment-1440299 Share on other sites More sharing options...
cpd Posted July 11, 2013 Share Posted July 11, 2013 No you don't need to use the StdClass. A variable has a name: $username = "cpd";. Why is this not satisfactory? Given your description for the problem, there's no need to use an object at all. @web_craftsman: from what you've suggested you don't seem to know what an object is for, when to use it, or how to use it. Quote Link to comment https://forums.phpfreaks.com/topic/280061-add-variable-to-an-object/#findComment-1440303 Share on other sites More sharing options...
web_craftsman Posted July 11, 2013 Share Posted July 11, 2013 @cpd: he wants "to include other things in it.". stdClass object is not a bad way to create some "advanced" variable if you don't want to use associative arrays. Quote Link to comment https://forums.phpfreaks.com/topic/280061-add-variable-to-an-object/#findComment-1440304 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.