在 Google 和这个网站上搜索了很长时间后,我非常紧张,似乎无法完成这项基本任务(Java 非常简单),但是使用 PHP 我很难。有人可以告诉我我做错了什么。我想要的只是将 Job 类传递到 Person 类中,并将它们链接在一起,但我正在努力掌握它,尝试各种其他途径来实现它。
<?php
class Person{
public $name;
public function __construct($name, Job $job){
$this->name = $name;
$this->job = $job;
}
public function display(){
return $this->name . $this->job;
}
}
class Job{
public $job;
public function __construct($job){
$this->job = $job;
}
}
$job = new Job("Programmer");
$person = new Person("John");
echo $person->display();
?>
问题是,您是否需要将Job实例(作为依赖项)传递给Person对象,如果它仅与作业名称有关 - 就像在以下上下文中一样?我会说不是:
class Person {
/**
* Person name.
*
* @var string
*/
private $name;
/**
* Job name.
*
* @var string
*/
private $job;
/**
* Person's age.
*
* @var int
*/
private $age;
/**
* Person's place of birth.
*
* @var string
*/
private $birthPlace;
/**
* Job name.
*
* @param string $name Person name.
* @param string $job Job name.
* @param int $age Person's age.
* @param string $birthPlace Person's place of birth.
*/
public function __construct(string $name, string $job, int age, string $birthPlace) {
$this->name = $name;
$this->job = $job;
$this->age = $age;
$this->birthPlace = $birthPlace;
}
/**
* Get person's details.
*
* @return string
*/
public function getDetails() {
return $this->name . ', ' . $this->job . ', ' . $this->age . ', ' . $this->birthPlace;
}
}
use Person;
$person = new Person('TLG', 'programmer', 28, 'Paris');
echo $person->getDetails();
但是,如果将更具体的工作细节分配给一个人呢?然后可以使用Job实例:
/*
* Job.
*/
class Job {
/**
* Job name.
*
* @var string
*/
private $name;
/**
* Job location.
*
* @var string
*/
private $location;
/**
*
* @param string $name Job name.
* @param string $location Job location.
*/
public function __construct(string $name, string $location) {
$this->name = $name;
$this->location = $location;
}
/**
* Get the job name.
*
* @return string
*/
public function getName() {
return $this->name;
}
/**
* Get the job location.
*
* @return string
*/
public function getLocation() {
return $this->location;
}
}
/*
* Person.
*/
use Job;
class Person {
/**
* Person name.
*
* @var string
*/
private $name;
/**
* Job instance.
*
* @var Job
*/
private $job;
/**
*
* @param string $name Person name.
* @param Job $job Job instance.
*/
public function __construct(string $name, Job $job) {
$this->name = $name;
$this->job = $job;
}
/**
* Get person's details.
*
* @return string
*/
public function getDetails() {
return $this->name . ', ' . $this->job->getName() . ' in ' . $this->job->getLocation();
}
}
/*
* The call.
*/
use Job;
use Person;
$job = new Job('Programmer', 'London');
$person = new Person('John', $job);
echo $person->getDetails();
笔记:
现在,如果一个人可以有多个工作怎么办?然后你可以使用JobCollection:
/*
* Job.
*/
class Job {
// Same as above...
}
/*
* Job collection.
*/
use Job;
class JobCollection {
/**
* Jobs list.
*
* @var Job[]
*/
private $jobs = [];
/**
* Add a job.
*
* @return $this
*/
public function add(Job $job) {
$this->jobs[] = $job;
return $this;
}
/**
* Get all jobs.
*
* @return Job[]
*/
public function all() {
return $this->jobs;
}
}
/*
* Person.
*/
use JobCollection;
class Person {
/**
* Person name.
*
* @var string
*/
private $name;
/**
* Job collection.
*
* @var JobCollection
*/
private $jobs;
/**
*
* @param string $name Person name.
* @param JobCollection $jobs Job collection.
*/
public function __construct(string $name, JobCollection $jobs) {
$this->name = $name;
$this->jobs = $jobs;
}
/**
* Get the person name.
*
* @return string
*/
public function getName() {
return $this->name;
}
/**
* Get the person's jobs.
*
* @return Job[]
*/
public function getJobs() {
return $this->jobs->all();
}
/**
* Get person's details.
*
* @return string
*/
public function getDetails() {
$result = $this->getName() . '<br/>';
foreach ($this->jobs->all() as $job) {
$result .= $job->getName() . ' in ' . $job->getLocation() . '<br/>';
}
return $result;
}
}
/*
* The call.
*/
use Job;
use Person;
use JobCollection;
// Create some jobs.
$job1 = new Job('Programmer', 'London');
$job2 = new Job('Bartender', 'Paris');
// Create the job collection and add the jobs to it.
$jobs = new JobCollection();
$jobs
->add($job1)
->add($job2)
;
// Create the person.
$person = new Person('John', $jobs);
echo $person->getDetails();
该得墨忒耳定律,在极大地给出清洁代号会谈-不看的东西!, 指定您应该只注入类直接使用的依赖项。例如,您不应该传递具有中介作用的依赖项,例如,这些依赖项仅用于创建您需要调用其方法的其他对象。
在我回答的早期版本中,我给您的印象是在Person对象中使用Job实例来分配作业名称是错误的。使用Job实例是正确的。通过使用它,你不打破毁灭之王本身,而只是它的细微差别,你并不真的需要一个工作实例,以便将作业名分配到人对象(如在我的第一个例子),如果作业并不意味着更具体的情况(如第二个示例和评论中的好问题)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句