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

Class author is injected into the constructor of Question class am I correct? but how to call the Question class to get the author's name?

$question = new Question('What is PHP', 'Adam');
$question->getFirstname;

like this? I assume Question class inherited Author class so Question's instance can use the function of Author Class?

0

2 Answers 2

2

Simple

echo $question->getAuthor()->getFirstName();

Think of it this way if it helps

$author = $question->getAuthor();
echo $author->getFirstName();

Also note that you can't construct a Question with the string "Adam", you need to pass an instance of Author

$question = new Question('What is PHP', new Author('Adam', 'Lastname'));
Sign up to request clarification or add additional context in comments.

2 Comments

so the final would be like this? $question = new Question('What is PHP', new Author('Adam', 'Lastname')); echo $question->getAuthor()->getFirstName();
@jameslebron yes. You could also construct author beforehand, eg $author = new Author('Foo', 'Bar'); $q = new Question('Eh?', $author);
2

You could create a new method:

class Question 
{
    // ...
    function getAuthorFirstname()
    {
        return $this->author->getFirstname();
    }
}

$question = new Question(.., new Author(..., ...));
echo $question->getAuthorFirstname();

Or, if you don't really care about Law of Demeter or feel that it doesn't apply:

$question = new Question(.., new Author(..., ...));
echo $question->getAuthor()->getFirstname();

In the end it all comes down to striking a balance between information hiding and pragmatism.

13 Comments

ah this is method chaining right? so my code of calling the getFirstname is wrong? what if I wish not to to change anything in my Question class?
Is that really violating LoD? getAuthor() returns the Author instance so at that point, we're out of Question
In fact, using your getAuthorFirstName example implies that Question has knowledge of Author
getAuthor() simply returns a value of type Author. I don't see the LoD specifying that only primitive types may be returned. It doesn't make a lot of sense to "unwrap" all complex types and provide direct access methods to their primitive values, that just increases complexity and dependencies.
Qualifier: it does make sense to implement something like getAuthorFirstname() if you want to hide the internal implementation completely and not propagate knowledge of an Author object beyond this class. However, if the consumer of a Question object is expected to have knowledge of the Author type, it doesn't make sense to hide it at the cost of increased complexity.
|

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.