0

I want to have my inheritance but it outputs null. it was working but when I extends Patient to clinic it starts to output null. my teachers instruction was to have inheritance and I need help im still studying about this inheritance but its kind a complicated for me.

<?php
    class Patient{
    
        private $name;
        private $age;
        private $gender;
    
        public function record($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 extends Patient{
    
        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());
  • 1
    You defined `public function record(...)` to create a new Patient, not the constructor – brombeer Nov 11 '21 at 06:57
  • even if i change the 'public function record(...)' into __contructor it still output null – deeku ahlam Nov 11 '21 at 07:01
  • If you haven't already, enable error reporting/display ([How do I get PHP errors to display?](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display?rq=1)). You'd get an `Uncaught ArgumentCountError` when changing `record()` to `__construct()` since `new Clinic();` wouldn't supply any parameters. Keep `record()` and add new patients via that method – brombeer Nov 11 '21 at 07:07
  • Also not really sure why you'd need/want inheritance here, since Clinic and Patient don't really share common attributes. That code would also work if you didn't `extends Patient` – brombeer Nov 11 '21 at 07:14
  • i didnt have any errors but the problem is that it outputs null value.. did online compiler aren't automatically enable error reporting/display? – deeku ahlam Nov 11 '21 at 07:15
  • "_did online compiler aren't automatically enable error reporting/display?_" We have no idea which online compiler you used. _Usually_ they do enable error reporting. With the code you posted you get NULL because you don't use `record()` to add patients – brombeer Nov 11 '21 at 07:21
  • 1
    "_my teachers instruction was to have inheritance_" Inheritance here would make sense if you had a class Person and class Patient and class Doctor would extend the Person class. – brombeer Nov 11 '21 at 07:24

1 Answers1

0

A few things to change here. First, you called the function for a new patient record, but you use new Patient(). Rename the function to __construct to make it the class constructor. Now you can use new Patient(...).

Also, I think extending Patient in Clinic doesn't really make sense here, because you don't want to use the classes provided by Patient and extend them.

So remove extends Patient and your code should work now:

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

print_r($clinic->getPatients());

Output:

Array
(
    [0] => Patient Object
        (
            [name:Patient:private] => Patrick star
            [age:Patient:private] => 18
            [gender:Patient:private] => Male
        )

    [2] => Patient Object
        (
            [name:Patient:private] => Eugene Krab
            [age:Patient:private] => 28
            [gender:Patient:private] => Male
        )

)

Tested on PHP 7.4 and PHP 8.0

EinLinuus
  • 625
  • 5
  • 16
  • 1
    *my teachers instruction was to have inheritance* so I think inheritance is needed in some form or another. So just removing it may be right in this case, it's not what OP needs. – Nigel Ren Nov 11 '21 at 07:22
  • But with `extends` you extend it - so you have all your functions from the other (in your case `Patiend`) class in your `Clinic` class. You don't need and don't want that, because `Clinic` is a totally different class. Extending `Patient` would make sense if you have another type of a patient which uses the normal `Patient` functions but also needs a few new. You don't need `extends` in your case, it simply doesn't make sense and won't work. – EinLinuus Nov 11 '21 at 07:34
  • My comment was about that it sounds like they **need** to use inheritance, not about if it was correct or not in this case. I don't know the actual assignment, so I can't comment on how it could be used in a correct manner. – Nigel Ren Nov 11 '21 at 07:35