Solved

array of multiple employees or an array of 1 employee information

Posted on 2014-01-16
8
309 Views
Last Modified: 2014-01-25
from php oop tuturial
http://www.larryullman.com/books/php-advanced-and-object-oriented-programming/

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">
<head>
    <meta charset="utf-8">
    <title>Type Hinting</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
<?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:
$hr->addEmployee($e1);
$hr->addEmployee($e2);

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

?>
</body>
</html>

Open in new window

0
Comment
Question by:rgb192
  • 4
  • 2
  • 2
8 Comments
 
LVL 20

Assisted Solution

by:Mark Brady
Mark Brady earned 167 total points
ID: 39787473
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.
0
 
LVL 34

Assisted Solution

by:gr8gonzo
gr8gonzo earned 333 total points
ID: 39788837
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.
0
 
LVL 20

Expert Comment

by:Mark Brady
ID: 39788848
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.
0
 

Author Comment

by:rgb192
ID: 39789945
My question is how can a non static property store values of multiple objects
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 34

Accepted Solution

by:
gr8gonzo earned 333 total points
ID: 39789958
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...
0
 

Author Closing Comment

by:rgb192
ID: 39791498
thanks.  I will open a question about example of class that can hold an array
0
 

Author Comment

by:rgb192
ID: 39791695
I have a followup question about example of class that can hold an array

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28342292.html
0
 

Author Comment

by:rgb192
ID: 39809308
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28342292.html

I have a followup about

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

able to hold values from multiple instances.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Please show me easiest post form. 3 31
Amazon Redshift 2 25
converting numbers with php 3 27
Checking if varaible is empty 6 29
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now