Assign values to a class var

Discussion in 'PHP' started by dizyn, Dec 15, 2008.

  1. #1
    I want to assign a value to my class variable please see the code below. I want to assign an array to $items? can i do this?

    If i can do please help me out.

    thanks
    class myClass
    {
    	var $items;
    }
    
    $obj = new myClass;
    PHP:
     
    dizyn, Dec 15, 2008 IP
  2. dizyn

    dizyn Active Member

    Messages:
    251
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #2
    Solved:
    $obj->items = array();
     
    dizyn, Dec 15, 2008 IP
  3. misbah

    misbah Active Member

    Messages:
    265
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    68
    #3
    try this
    $your_array = array('foo','bar','hello','world');
    $obj->items = $your_array;
    PHP:
     
    misbah, Dec 15, 2008 IP
  4. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #4
    I wouldn't do that, you are removing an important aspect of OOP (using control coupling).

    I would assign a function to handle the values and not allow externals to manipulate it except through the functions.

    Peace,
     
    Barti1987, Dec 15, 2008 IP