I'm trying to learn to use dependency injection, but am having problems putting it to use. I understand what is happening with the two classes here, however when I go to use them, I'm doing something wrong.
class Author
{
private $firstname;
private $lastname;
public function __construct($firstname, $lastname)
{
$this->firstName = $firstName;
$this->lastName = $lastName;
}
public function getFirstName()
{
return $this->firstName;
}
public function getLastName()
{
return $this->lastName;
}
}
class Question
{
private $author;
private $question;
public function __construct($question, Author $author)
{
$this->author = $author;
$this->question = $question;
}
public function getAuthor()
{
return $this->author;
}
public function getQuestion()
{
return $this->question;
}
}
Okay there are the two basic classes uses dependency injection. I understand all of that, but if I want to use them. I have tried this and I get an error.
$author = new Author('Mickey', 'Mouse');
$author->getFirstName();
$question = new Question('what day is it?', $author);
$question->getQuestion();
I want it to have outputted 'Mickey' and 'what day is it?', but instead I get the following error.
Undefined variable: firstName in /var/www/OOP/dependency-injection/example1.php on line 12
Undefined variable: lastName in /var/www/OOP/dependency-injection/example1.php on line 13
Why am I getting this error? I thought I declared the variables here $author = new Author('Mickey', 'Mouse');?
Thanks for any insights.