1

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.

2
  • 1
    Get an IDE with code completion, and keep in mind PHP is case sensitive. Pick a style like camelCase and stick to it ;) Commented Dec 11, 2013 at 17:33
  • 4
    This question appears to be off-topic because it is about debbuging code and will have no value for future visitors. Commented Dec 11, 2013 at 17:35

2 Answers 2

4

your firstname and firstName do not match up in your Author class

public function __construct($firstname, $lastname)
{
    $this->firstName = $firstName;
    $this->lastName = $lastName;
}

should be

private $firstName;
private $lastName;
public function __construct($firstName, $lastName)
    {
        $this->firstName = $firstName;
        $this->lastName = $lastName;
    }
Sign up to request clarification or add additional context in comments.

5 Comments

Also, while it will work the way it is, OP needs to update the class vars to be same case. Right now he has private $firstname; should be private $firstName;
Wow I can't believe I didn't see that before! Thanks. I have changed my variables, but now I see a blank screen. It is because I am only returning the variables inside of the functions instead of echoing the results?
Well there's nothing in the code you've shown us that would print anything to the screen. Functions should always return values, NEVER echo them. If you know enough to ask if it's because you didn't echo, it seems like you know the answer to your question, or could at least try some basic debugging...
You're right Jessica, I just needed to play around with it some more. All fixed! Thank you :)
@Mitch: re getting a blank screen, there's nothing here that needs to be run on a web server. You may find it easier to play with this stuff at the console - it cuts out stuff you don't need.
1

You have declared private $firstname, and inside the getter you are calling $this->firstName capitalized.

1 Comment

That's not the immediate problem, the problem is the constructor's arguments are all lowercase ($firstname) and then he tries to use camelcase to access them ($firstName);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.