viion Posted October 6, 2009 Share Posted October 6, 2009 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]; Quote Link to comment https://forums.phpfreaks.com/topic/176756-solved-using-arrays-to-classes/ Share on other sites More sharing options...
nafetski Posted October 6, 2009 Share Posted October 6, 2009 $item_list =array("item1", "item2", "item3"); $user->setList($item_list); Try that Quote Link to comment https://forums.phpfreaks.com/topic/176756-solved-using-arrays-to-classes/#findComment-931945 Share on other sites More sharing options...
mikesta707 Posted October 6, 2009 Share Posted October 6, 2009 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; } Quote Link to comment https://forums.phpfreaks.com/topic/176756-solved-using-arrays-to-classes/#findComment-931947 Share on other sites More sharing options...
viion Posted October 6, 2009 Author Share Posted October 6, 2009 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]; Quote Link to comment https://forums.phpfreaks.com/topic/176756-solved-using-arrays-to-classes/#findComment-931964 Share on other sites More sharing options...
viion Posted October 6, 2009 Author Share Posted October 6, 2009 I finally got this working, my error was a silly typo in another piece of code. Heh, proof reading pays off. Quote Link to comment https://forums.phpfreaks.com/topic/176756-solved-using-arrays-to-classes/#findComment-932017 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.