0

I'm having difficulties to understand the given code and the reason behind dependency injection. I've got the following error:

Uncaught Error: Call to undefined method Question::getFullName() in C:\xampp\htdocs\OOP\Index.php:10 Stack trace: #0 {main} thrown in C:\xampp\htdocs\OOP\Index.php on line 10.

Even if I instantiate an object of the Author class in the constructor, I keep getting a string in the Question class once I try to use getQuestion().

require 'Author.php';

class Question {
  private $author;
  private $question;

  public function __construct($question, Author $author) {
    $this->author = $author;
    $this->question = $question;
  }

  public function getAuthor() {
    $firstname = $this->author->getFirstName();
    $lastname = $this->author->getLastName();
    $fullaname = $firstname . $lastname;

    return $this;
  }

  public function getQuestion() {
    return $this->question;
  }
}

<?php

class Author {
  private $firstName;
  private $lastName;
  private $fullName;

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

  public function getFirstName() {
    return $this->firstName;
  }

  public function getLastName() {
    return $this->lastName;
  }

  public function getFullName() {
    return $this->fullName = $this->firstName." ".$this->lastName;
  }
}
require 'Question.php';

$question = new Question("What is the author's name?", new Author("josel", "parayno"));

echo $question->getQuestion();
echo $question->getFullName();

1 Answer 1

2

$question really does not have getFullName method. Method getFullName exists in class Author. And after creating and "sending" to Question, when it was created method getFullName available in class Question private $author property.

But if you want to get Athor name by follow code, you need try

$question->getAuthor()->getFullName();

And if you do this, you take error again, becouse in question->getAuthor you return $this, and in this case this is a Question object. For getting author name from question object you should to do follow:

  1. Fix getAuthor like this
public function getAuthor() 
{
    $firstname = $this->author->getFirstName();
    $lastname = $this->author->getLastName();
    return $this->author;
}
  1. Rewite call name in you index.php like this

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

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

2 Comments

In fact - getAuthor can JUST return $this->author - the rest doesn't do anything useful in this case.
I am sure, in this case method getAuthor provide working with protection method by program rules. In other cases getter like that getAuthor and setters too provide very usefull and safe working with class methods and centralization of property management.

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.