Ey guys, I am trying to learn Dependency Injection and I wrote this code:
class User {
public $id;
public function __construct($id) {
$this->id = $id;
}
public function getName() {
return 'Alex';
}
}
class Article {
public $author;
public function __construct(User $author) {
$this->author = $author;
}
public function getAuthorName() {
return $this->author->getName();
}
}
$news = new Article(10);
echo $news->getAuthorName();
However, I am getting WSOD. What had I done wrong in it ?
10instead ofUserobject.Articlerequires a user, not an integer; instead, you should do this:$news = new Article(new User(10)); echo $news->getAuthorName();WSOD? Also$news = new Article(10);->$news = new Article(new User(10));