Home > Back-end >  how to delete object in a constructor in PHP?
how to delete object in a constructor in PHP?

Time:11-11

I have my code here and I dont know how to delete object with this kind of method $patient->deletePatient(2) im a beginner and I cant find an answer in internet and I think the way i use var_dump was incorrect please help im stuck this is my code:

<?php
$index = 0;

class Clinic { 
    public $name;
    public $age;
    public $gender;
      
      
    function Patient($name,$age,$gender){
      $this->name = $name;
      $this->age = $age;
      $this->gender = $gender;
      
      $id = $name;
      $id = $age;
      $id = $gender;
    }

    function assignPatient($name,$age,$gender){
        $this->Patient($name,$age,$gender);
      }
      
    function deletePatient($id=0){

        $this->Patient($id);
        var_dump((unset) $id);

    }

}


$patient = new Clinic;

$patient->assignPatient("Patrick star",18,"Male");
$patients[$index] = $patient;
$index  ;
$patient->assignPatient("SpongeBob Squarepants",17,"Male");
$patients[$index] = $patient;
$index  ;
$patient->assignPatient("Eugene Krab",28,"Male");
$patients[$index] = $patient;
$index  ;
$patient->deletePatient(2);
foreach($patients as $patient)
    {
     
        echo $patient->name . " ";
        echo $patient->age . " ";
        echo $patient->gender . "\n"; 
      
        
      
    }
  

CodePudding user response:

Patient and Clinic should be separate objects. Then you can store Patient objects inside Clinic->patients array.

class Patient{

    private $name;
    private $age;
    private $gender;

    public function __construct($name, $age, $gender){
        $this->name = $name;
        $this->age = $age;
        $this->gender = $gender;
    }   

    public function getName(){
        return $this->name;
    }

    public function getAge(){
        return $this->age;
    }

    public function getGender(){
        return $this->gender;
    }

}

class Clinic{

    private $patients = [];

    public function getPatients(){
        return $this->patients;
    }

    public function assignPatient($name, $age, $gender){
        $this->patients[] = new Patient($name, $age, $gender);
    }

    public function deletePatient($index){
        unset($this->patients[$index]);
    }

}

$clinic = new Clinic();

$clinic->assignPatient("Patrick star",18,"Male");
$clinic->assignPatient("SpongeBob Squarepants",17,"Male");
$clinic->assignPatient("Eugene Krab",28,"Male");

$clinic->deletePatient(1);

var_dump($clinic->getPatients());

CodePudding user response:

<?php
class Patient {
    public $name;
    public $age;
    public $gender;

    function __construct($name, $age, $gender) {
        $this->name = $name;
        $this->age = $age;
        $this->gender = $gender;
    }
}

class Clinic {
    private $patients = [];
    private $patientIndex = 0;

    function assignPatient($patient) {
        $this->patients[$this->patientIndex] = $patient;
          $this->patientIndex;

        return $this->patientIndex;
    }

    function deletePatient($id) {
        if(array_key_exists($id, $this->patients) {
            unset($this->patients[$id]));
            return true;
        }
        return false;
    }

    function getPatients() {
        return $this->patients;
    }
}

$clinic = new Clinic();
$patient1 = new Patient("Patrick star",18,"Male");
$id1 = $clinic->assignPatient($patient1);
$patient2 = new Patient("SpongeBob Squarepants",17,"Male");
$id2 = $clinic->assignPatient($patient2);
$patient3 = new Patient("Eugene Krab",28,"Male");
$id3 = $clinic->assignPatient($patient3);
$clinic->deletePatient($id2);

foreach($clinic->getPatients() as $patient) {
    var_dump($patient);
}
  • Related