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
https://forums.phpfreaks.com/topic/280061-add-variable-to-an-object/
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.

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. 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.