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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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.