Create one to many relationship between two classes in core PHP

Viewed 31

Kindly can someone guide me on how to achieve this?

  • Create a class Company and define attributes Company Name, Company Address.
  • Create a class Employees and define attributes Employee Name, Phone Number, and Email.
  • Create a relationship between the above two classes where a Company can have multiple Employees in such a way as to retrieve company and its employee information through its Company object.
1 Answers

You need to create Employee class:

class Employee 
{
    public $name
    public $phoneNumber
    public $mail
}

and Company class:

class Company 
{
    public $name;
    public $address;
    public $matchCount;
    public $employees = array();
}

You can use the following snippet:

$company = new Company();
$company->employees[0] = new Employee();
Related