array of multiple employees or an array of 1 employee information

from php oop tuturial

How can class department $this->_employees[] hold an array of employees
or is it just an array of 1 employee information

<!doctype html>
<html lang="en">
    <meta charset="utf-8">
    <title>Type Hinting</title>
    <link rel="stylesheet" href="style.css">
<?php # Script 6.8 - hinting.php
//  This page defines and uses the Department and Employee classes. 

# ***** CLASSES ***** #

/* Class Department.
 *  The class contains two attribute: name and employees[].
 *  The class contains two methods: 
 *  - __construct()
 *  - addEmployee()
class Department {
    private $_name;
    private $_employees;
    function __construct($name) {
        $this->_name = $name;
        $this->_employees = array();
    function addEmployee(Employee $e) {
        $this->_employees[] = $e;
        echo "<p>{$e->getName()} has been added to the {$this->_name} department.</p>";
} // End of Department class.

/* Class Employee.
 *  The class contains one attribute: name.
 *  The class contains two methods: 
 *  - __construct()
 *  - getName()
class Employee {
    private $_name;
    function __construct($name) {
        $this->_name = $name;
    function getName() {
        return $this->_name;
} // End of Employee class.

# ***** END OF CLASSES ***** #

// Create a department:
$hr = new Department('Human Resources');

// Create employees:
$e1 = new Employee('Jane Doe');
$e2 = new Employee('John Doe');

// Add the employees to the department:

// Delete the objects:
unset($hr, $e1, $e2);


Open in new window

Who is Participating?
gr8gonzoConnect With a Mentor ConsultantCommented:
It just does. It doesn't matter if an array is static or not. An array is an array, and it can hold multiple values or multiple objects...
Mark BradyConnect With a Mentor Principal Data EngineerCommented:
I don't see the point in these classes. As soon as a value is set for the clases they are unset. Doesn't make any sense.

In answer to your question and the way this sample is written there will only ever be 1 employee in that array.

$e1 and $e2 are two completely different instances of the employee class and only one employee is added to each one.

This is not the way you would setup these classes at all.

$hr is a single instance of the department class.
$hr->addEmployee() takes a single argument but you should be sending in the actual employee "name" not the class instance (object) of the employee.

In any case this is a poor example of working with OOP (classes) in php.

Perhaps if you tell us what it is you would like to achieve we could give you a better example.
gr8gonzoConnect With a Mentor ConsultantCommented:
1. I've read elvin66's comments a few times, but I'm not sure he and I are looking at the same code (or maybe my coffee hasn't kicked in yet).

Elvin, I think rgb192 is going through some tutorials, so the unset() is just a cleanup step for the test script. I don't see why you would send in an employee name vs. the object instance. The addEmployee method takes a single argument that HAS to be an Employee instance. This is a type hinting tutorial.

2. I don't understand the original question.

This line:
$this->_employees[] = $e;

...adds the instance of an employee to the the department's array of employees. So I'm trying to understand what your actual question is, since the code looks correct.
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Mark BradyPrincipal Data EngineerCommented:
Actually gr8gonzo you are correct. It was me that hadn't had my coffee before reading this question! Thanks for the jab :)

I guess we just need to know what the end goal is.
rgb192Author Commented:
My question is how can a non static property store values of multiple objects
rgb192Author Commented:
thanks.  I will open a question about example of class that can hold an array
rgb192Author Commented:
I have a followup question about example of class that can hold an array
rgb192Author Commented:

I have a followup about

line 28 $this->_employees[] = $e;

able to hold values from multiple instances.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.