php - Access variable from outrside of class
Get the solution ↓↓↓Solution:
Pass in the array as an argument of theword
constructor:
class word {
private $words;
public function __construct(array $words) {
$this->words = $words;
}
// rest of class here
}
// When translating:
$word = new word($translate);
$word->translate('fr', $string);
This concept is known as dependency injection, however I would do it like this in case you have more arrays to pass:
class word {
private $words = array();
public function __construct(array $words) {
$this->addWords($words);
}
public function addWords(array $words) {
$this->words = array_merge($this->words, $words);
}
// rest of class here
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: foreign key constraint is incorrectly formed laravel
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.