php - 在php中分配对象
问题描述
我有一个 Java 代码,它创建了一个我有兴趣在我的 php 项目中实现的函数。
此代码示例(由于原件较长,因此不完整)将患者分配给医生,它们是两个医生和患者对象。
我试图在 PHP 中做同样的事情,但我不知道如何继续,因为语法完全不同,我暂时离开我所做的。
Java 中的代码。
public class Doctor {
private String name;
private TreeSet<patient> assignedPatient;
public Doctor(String name) {
this.name = name;
this.assignedPatient = new TreeSet<>;
}
public boolean assignPatien(Patient p) {
return this.assignedPatient.add(p);
}
public boolean assignPatient(String name, String name2) {
Doctor d = null;
Patient p = null;
Iterator itDoctor = doctor.iterator();
Iterator itPatient = patient.iterator();
while (itDoctor.hasNext()) {
Doctor aux = (Doctor) itDoctor.next();
if (aux.getName().equals(name)) {
d = aux;
break;
}
}
while (itPatient.hasNext()) {
Patient aux = (Patient) itPatient.next();
if (aux.getName().equals(name2)) {
p = aux;
break;
}
}
if (d != null && p != null) {
return d.assignPatient(p);
} else {
System.out.println("The patient could not be assigned");
return false;
}
}
PHP 中的代码。(我不知道如何从这里继续)
class Doctor {
private $name;
private $assignedPatient;
public function __construct($name){
$this->name = $name;
$this->assignedPatient = array();
}
}
解决方案
纯娱乐 ...
<?php
class Hospital {
public $patients = array();
public $doctors = array();
public function __constructor() {
}
public function admitPatient($p) {
$this->patients[] = $p;
}
public function employDoctor($d) {
$this->doctors[] = $d;
}
public function pageDoctor($n) {
foreach($this->doctors as $d) {
if($d->name == $n) return ($d);
}
return false;
}
public function fetchPatient($n) {
foreach($this->patients as $p) {
if($p->name == $n) return ($p);
}
return false;
}
public function assignPatient($pName, $dName) {
$patient = $this->fetchPatient($pName);
$doctor = $this->pageDoctor($dName);
if($patient && $doctor) {
$patient->doctor = $doctor;
$doctor->patients[] = $patient;
return true;
}
return false;
}
}
class Patient {
public $name;
public $doctor;
public function __construct($name){
$this->name = $name;
$this->doctor = false;
}
}
class Doctor {
public $name;
public $patients;
public function __construct($name){
$this->name = $name;
$this->patients = array();
}
}
$hospital = new Hospital();
$doc1 = new Doctor("Mr Hyde");
$doc2 = new Doctor("Doc Jimmy");
$patient1 = new Patient("Sick Boy");
$patient2 = new Patient("John Dying");
$hospital->employDoctor($doc1);
$hospital->employDoctor($doc2);
$hospital->admitPatient($patient1);
$hospital->admitPatient($patient2);
$hospital->assignPatient("Sick Boy", "Mr Hyde");
$hospital->assignPatient("John Dying", "Doc Jimmy");
var_dump($hospital);
推荐阅读
- javascript - AWS s3 存储桶上的 React-Router 位置不起作用
- r - 如何计算R中零之间的值?
- database - 将索引放在 postgres 中经常更新的列上
- python - 如何从python中的url正确获取数据帧?
- javascript - 使用 JSON 从给定的外部 json 对象中使用 async/await 方法填充选择下拉列表,并在 vanilla js 中尝试和捕获语句
- r - 使用带有 MODIStsp R 包的 shapefile
- javascript - 从没有循环的键值的json中替换html中的文本
- azure-storage - Azure 存储生命周期管理筛选器容器
- python - How to build a DQN that outputs 1 discrete and 1 continuous value as a pair?
- python - How to solve python+flask+mongodb ServerSelectionTimeoutError?