Link to home
Start Free TrialLog in
Avatar of Charles Baldo
Charles BaldoFlag for United States of America

asked on

PHP Dependency injection want to get value out

I am very new to PHP, learning dependency Injection.  I have created two classes Author and Question and have injected Author into question.

I want to get echo out of it "What is your favorite Book John Public"

Here is my coded right now

<?php

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

$AuthorObj = new Author("John","Public");

$QuestionObj = new Question("What is your favorite book ",$AuthorObj);

echo $QuestionObj->getQuestion();

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mark Brady
Mark Brady
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Charles Baldo

ASKER

Thanks Mark,  I get it now
I am not sure if your question has really been addressed here. Given the question was about DI the solution provided does not use DI to get the required output
The change recommended for line 26 was unnecessary. In your code you have used type hinting (Author) correctly in your constructor - there is no reason to remove it.

Adding line 41 does not tie in with the dependency injection side of the question.

Dependency Injection is about keeping your objects fluid - instead of instantiating dependencies inside the object where they are consumed you instead "inject" them into the object from a factory or parent object. The reasons for doing this are many - the main ones are that it provides flexibility in terms of function - by passing in different instances of objects you can achieve different results - this makes code easier to test. For example I can inject a database object into my Client object for testing and have it work against a test environment - I can then inject a production Database object when I want to go live. My client object remains the same - in terms of its functionality.

Applying this to your situation. It is not clear how you want to link the output you want to DI but I am going to assume that it is when you call the getQuestion() on the Question object. The question is combined with the injected author details to give the desired result.

We can do this either in the ctor of the Question object or in the getQuestion() method - again it depends on the final intended function of the Question object.
Here is an example of how this would be done. I have changed the constructor of the Question object only - adding the author's details to the question. This may or may not be the correct place to put it as I have mentioned but it does demonstrate how to use the injected value in the receiving object.
<?php

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;
        // We can construct the question here or in the get Question
        // method - it depends on what this object is meant to do
        $this->question = $question . $author->getFirstName() . ' ' . $author->getLastName();
    }
 
    public function getAuthor() {
        return $this->author;
    }
 
    public function getQuestion() {
        return $this->question;
    }

}

$AuthorObj = new Author("John","Public");

$QuestionObj = new Question("What is your favorite book ",$AuthorObj);

echo $QuestionObj->getQuestion();

Open in new window

Thanks Julian,

It did give me what I was looking for, most important to understand.  I have tried a few variations like including a getFullName() method and actually tried what you suggested

Right now I am the mighty acorn with PHP. Normally a c# programmer.  I am on a project that they acquired a PHP package with no expertise!!! Someone asked me if I wanted to do it and i jumped at it.  So everything I am doing right now is research, no real objective at t his point except to learn it.

I appreciate your expert advice, you have given me some real gems in the past with jQuery. thanks
I have tried a few variations like including a getFullName() method and actually tried what you suggested
Ok, just needed to clarify the DI aspect of it - the solution provided does not use DI (the same result could have been obtained WITHOUT injecting the Author object into the Question - rendering the inclusion of the author object null and void) - hence my post.
Thanks for accepting my answer. I was at work and didn't have time to go into a full explanation and a better way to achieve your goal but merely pointed out how to get the result you wanted using your code. Of course there are much better ways to achieve the same outcome that would be cleaner but in your PHP studies you will find that there are many ways to do the same thing. Some are industry standard while others are not. It is up to you to determine how you want to code moving forward. My advice is keep learning and reading tutorials and you will pickup most of these logical ideas. Good luck in your studies!
Thank you both.  I am on day 3 of learning PHP after doing c# for almost 20 years.   I am enjoying it and appreciate any help and comments I get.