Rapprogtrain
Rapprogtrain

Reputation: 155

Arrays in php oop

I have array, where i put data depend on the url. But there is the problem, i can not print this array like in the simple php:

$array = ["hi", "name"]; echo $array[1];

what is wrong in my code that i will show, and how i can print the array

Code:

<?php

class Translate {

    public $transl = [];

    public function getTransl($transl = []) {
        if (( isset($_GET['lang']))) {
    if ($_GET['lang'] == "en") {
        $this->transl = ['word1', 'word2'];
        }
        if ($_GET['lang'] == "ru") {
            $this->transl = ['word3', 'word4'];
            }
}
    }

}

$test = new Translate();
$test->getTransl([0]);

?>

Upvotes: 1

Views: 117

Answers (4)

Nigel Ren
Nigel Ren

Reputation: 57131

There are a few issues with your code as others have pointed out. But from a code structure point of view, there are some more fundamental issues (IMHO).

If you are going to create a translator, basing it on if $_GET variables means it can be difficult to test. In this example, you send in a language you want in the constructor and then the class will just set the private translator variables to the table of translations.

Secondly - using numeric values for the word your after can be prone to errors (so can this method, but less so). In this case, the key of the translation is the word you want to start with and the value is the new word, so rather than

echo $test->getTransl(0);

you use

echo $russianTrans->getTransl("word2");

This is the code, hope it helps...

class Translate {
    // Use private variables whenever possible
    private $transl = [];

    public function __construct( string $lang ) {
        if ($lang == "en") {
            $this->transl = ['word1' => 'word1', 'word2' => 'word2'];
        }
        if ($lang == "ru") {
            $this->transl = ['word1' => 'word3', 'word2' => 'word4'];
        }
    }

    public function getTransl($word) {
        return $this->transl[$word];
    }

}

$russianTrans = new Translate($_GET['lang']); // Or hardcode it to 'ru' for example
echo $russianTrans->getTransl("word2");

Upvotes: 0

devpro
devpro

Reputation: 16117

No idea, why are you using $transl = [] in method parameter when you need specific index, here you can just pass key which you need.

Example:

<?
class Translate {

    public $transl = 0;

    public function getTransl($transl = '') {
      if (( isset($_GET['lang']))) {
        if ($_GET['lang'] == "en") {
          $this->transl = ['word1', 'word2'];
        }
        if ($_GET['lang'] == "ru") {
            $this->transl = ['word3', 'word4'];
        }
      }
      return $this->transl[$transl];
    }
}

$test = new Translate();
echo $test->getTransl(0); // this will print `word1` if $_GET['lang'] equal to `en`
?>

In your code, you are not using either echo or return in your method to get the result, and you are not matching $transl with $this->transl anywhere to get the specific index.

Upvotes: 1

Michel Kapelle
Michel Kapelle

Reputation: 149

I think you'll just need to return the output.

Let's say you have a file named test.php on your server

class Translate {

    public $transl = [];

    public function getTransl($transl = []) {
        if (( isset($_GET['lang']))) {
            if ($_GET['lang'] == "en") {
                $this->transl = ['word1', 'word2'];
            }
            if ($_GET['lang'] == "ru") {
                $this->transl = ['word3', 'word4'];
            }
        }
        return $this->transl;
    }

}

$test = new Translate();
$output=$test->getTransl([0]);
echo "<pre>";
    print_r($output);
echo "</pre>";

Running http://server/{{enterfolderhere}}/test.php?lang=en in your browser will give

Array
(
    [0] => word1
    [1] => word2
)

Running http://server/{{enterfolderhere}}/test.php?lang=ru in your browser will give

Array
(
    [0] => word3
    [1] => word4
)

Upvotes: 0

kainaw
kainaw

Reputation: 4334

First, you don't pass the index as a parameter. You use it as an index. Proper syntax would be:

$test->getTransl()[0];

That assumes that $test->getTransl() returns an array. But it doesn't. It doesn't return anything. It just sets the class attribute $transl. So, you have to do it in two lines:

$test->getTransl(); // This sets the attribute
$test->transl[0]; // This uses the attribute

But, that goes against that the method implies. The method implies that it returns the transl attribute. So, you SHOULD return it in the function with:

return this->transl;

Then, you can use:

$test->getTransl()[0];

Of course, this won't print anything. You need to precede with with echo or print:

echo $test->getTransl()[0];

Upvotes: 0

Related Questions