简体   繁体   中英

PHP Dependency Injection

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM