我有一个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();
}
}
答案 0 :(得分:1)
只是为了好玩...
<?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);