1

I am trying to understand a tutorial about PHP Dependency. Below is the example codes:-

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;
    }
}

To understand how it works, I've added the following codes:-

$author = new Author('John', 'Doe');
$qn = new Question('What is your name?', $author);
$authorName = $qn->getAuthor();
print_r($authorName);

That returns me the following:-

Author Object
(
    [firstName:Author:private] => John
    [lastName:Author:private] => Doe
)

How can I access the value of the Author object, ie. firstName and lastName?

I tried $authorName->firstName but that returns me with an error

Cannot access private property Author::$firstName

Like I said, I am just trying to get my head with dependency injection using this code example. I know that by changing the type $firstName to public in the Author class will make it work but still need clarifications from any PHP gurus if its the code example is incorrect or my code to retrieve the value is incorrect.

Thanks and really appreciate any feedbacks.

2 Answers 2

0

The paradigm here is that for each "private" field there is a corresponding "public" function to obtain that field. This is how you are getting your Author from your $qn object: by calling $qn->getAuthor() so similarly, since that gives you an author and you want the author's first name, you would use

$author = $qn->getAuthor();
$firstname = $author->getFirstName();

The dependency injection is simply mandating that when you make a Question, the second argument you pass to the constructor must be of type Author

Sign up to request clarification or add additional context in comments.

3 Comments

the way you call it is a bit awkard, that;s why I call the Author inside the Question Class. just like $question->getAuthor() rather than $author->getFirstName().
To me, using the $author object to retrieve the first and last names defeats the purpose of adding the dependency injection into the Question class. If the purpose of this example classes is to simply show how this is done, then so be it. On face value, am I correct to assume that even though the examples shows how dependency injection is done, there is no way for me to retrieve the values from within the Question class without actually modifying or adding the method within the Question class itself?
@asyadiqin You have to see it more like you are defining a contract that the Question always contains an Author. So if I use an Question instance I am always able to get its related author. To actually fetch the firstName you would really do something like $qn->getAuthor()->getFirstName().
-1

If you want to access the author name from the Question class you can modify your getAuthor function:

Class Question getAuthor()

public function getAuthor() {
    // call the author class then call the method you want to retreive
    // call the author info by using any proterties inside the 
    //author class ex. $this->author->getFirstName()
    return $this->author->getFirstName().' '.$this->author->getLastName();
}

by calling

$authorName = $qn->getAuthor();
print_r($authorName);
// you will get John Doe

3 Comments

Since the Question class contains an Author instance, I would instead recommend making a third method called getAuthorName rather than changing the one that is supposed to return the Author instance
Thanks guys. So back to my original question .... is there a way for me to retrieve the author's name, either first, last or both... without modifying the 2 class examples and only by using the $qn object?
hi @asyadiqin unfortunately you really need to edit one of them. try implementing my answer. Then maybe you will have an idea? cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.