Jump to content

[SOLVED] using arrays to classes


viion

Recommended Posts

I have a class setup and I want a var that is in the class to hold an array.

 

This is how it's setup:

 

class items {
var $item_list;

function setList($d1) {
	$this->item_list = $d1;
}
}
$user = new items;

Then upon loading the website this runs:

 

$item_list==("item1", "item2", "item3");
$user->setList(array($item_list));

when I try pull one of the items though nothing seems to happen. Like this:

 

$user->item_list[1];

Link to comment
https://forums.phpfreaks.com/topic/176756-solved-using-arrays-to-classes/
Share on other sites

btw the var keyword in PHP 5 is acceptable but deprecated. You should use public/private/protected.  but your code is all wrong

 

$item_list = array("item1", "item2", "item3");
$user->setList($item_list);

 

try that.

 

 

beat me again.

btw you may want to have a method that can append to the list

public function append($d1) {
$this->item_list[] = $d1;
}

 

I changed my statement (the == was a typo on here >.<)

Removed the Array command in the $user->setList

Added public function

 

Still nothing working

 

class items {
    var $item_list;
    public function setList($d1) {
    	$this->item_list = $d1;
    }
}
$user = new items;


$item_list=array("item1", "item2", "item3");
$user->setList($item_list);


echo $user->item_list[1];

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.