首页 > 解决方案 > 在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

解决方案


纯娱乐 ...

<?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);

推荐阅读