Jump to content

Add variable to an Object


ttmt_101

Recommended Posts

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;


?>

Link to comment
Share on other sites

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 by cpd
Link to comment
Share on other sites

@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 by ttmt_101
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.