Jibberish Posted May 18, 2008 Share Posted May 18, 2008 Hi there, I'm quite new to php so sorry if this is a silly mistake. basically i have a class which looks kind of like this class someClass { private $array = array(); function __construct() { $array[1] = "hello"; $array[2] = "goodbye"; } function getGreeting ($i) { return $this->array[$i]; } } $greeting = new someClass(); echo $greeting->getGreeting(1); When i do this, instead of getting "hello" it outputs nothing. any help would be great, thanks in advance. Link to comment https://forums.phpfreaks.com/topic/106209-solved-array/ Share on other sites More sharing options...
GingerRobot Posted May 18, 2008 Share Posted May 18, 2008 Try: <?php class someClass { private $array = array(); function __construct() { $this->array[1] = "hello"; $this->array[2] = "goodbye"; } function getGreeting ($i) { return $this->array[$i]; } } $greeting = new someClass(); echo $greeting->getGreeting(1); ?> You always need the to use $this-> when using class attributes. Link to comment https://forums.phpfreaks.com/topic/106209-solved-array/#findComment-544384 Share on other sites More sharing options...
Jibberish Posted May 18, 2008 Author Share Posted May 18, 2008 cant believe i did that, been looking at that for ages as well and it didn't even appear to me. Thanks a lot, you saved me from going insane. Link to comment https://forums.phpfreaks.com/topic/106209-solved-array/#findComment-544386 Share on other sites More sharing options...
GingerRobot Posted May 18, 2008 Share Posted May 18, 2008 No worries. It's usually the simple things that you don't spot. Can you mark the topic as solved if you're all done? Link to comment https://forums.phpfreaks.com/topic/106209-solved-array/#findComment-544393 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.