Jump to content

[SOLVED] array


Jibberish

Recommended Posts

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

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

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.