...

/

Solution Review: Dependency Injection

Solution Review: Dependency Injection

Let's look at the solution of the Dependency Injection challenge.

Solution: Task 1

Press + to interact
<?php
class Article {
protected $title;
protected $author;
public function __construct($title, $author) {
$this -> title = $title;
$this -> author = $author;
}
public function getTitle() {
return $this -> title;
}
public function getAuthor() {
return $this -> author;
}
}
class Author {
protected $name;
public function setName($name) {
$this -> name = $name;
}
public function getName() {
return $this -> name;
}
}
function test()
{
$author1 = new Author();
$author1 -> setName("Joe");
$title = "To PHP and Beyond";
$article1 = new Article($title,$author1);
return $article1 -> getTitle() . ' by ' . $article1 -> getAuthor() -> getName();
}
echo test();
?>

Explanation

  • Line 2: We write the Article class with protected properties $title and $author.
  • Line 6: We add the __construct() method to the Article class, which
...