Jump to content

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];

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.