non object calling methods

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Iterators</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
<?php # Script 8.9 - iterator.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 seven methods now! 
 */
class Department implements Iterator {
    private $_name;
    private $_employees;

    // For tracking iterations:
    private $_position = 0;
    
    function __construct($name) {
        $this->_name = $name;
        $this->_employees = array();
        $this->_position = 0;
    }
    function addEmployee(Employee $e) {
        $this->_employees[] = $e;
        foreach ($this->_employees as $name_them){
          echo '<br>name: '.$name_them->getName();
        }
        echo "<p>{$e->getName()} has been added to the {$this->_name} department.</p>";
    }
    function __toString(){
      return $var;
    }

    // Required by Iterator; returns the current value:
    function current() {
        return $this->_employees[$this->_position];
    }

    // Required by Iterator; returns the current key:
    function key() {
        return $this->_position;
    }
    
    // Required by Iterator; increments the position:
    function next() {
        $this->_position++;
    }

    // Required by Iterator; returns the position to the first spot:
    function rewind() {
        $this->_position = 0;
    }

    // Required by Iterator; returns a Boolean indiating if a value is indexed at this position:
    function valid() {
        return (isset($this->_employees[$this->_position]));
    }
    
} // End of Department class.

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');
$film = new Department('last_time');
// Create employees:
$e1 = new Employee('Jane Doe');
$e2 = new Employee('John Doe');
$e3 = new Employee('amy');
// Add the employees to the department:
$hr->addEmployee($e1);
$hr->addEmployee($e2);
$hr->addEmployee($e3);
$film->addEmployee($e1);
// Loop through the department:
echo "<h2>Department Employees</h2>";
foreach ($hr as $e) {
    echo "<p>{$e->getName()}</p>";
}

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

?>
</body>
</html>

Open in new window


code works but
I do not understand how a non object can call methods

line 33, line 35

or maybe just line 33

$name_them was not declared as an object
maybe $e is an object
LVL 1
rgb192Asked:
Who is Participating?
 
gr8gonzoConnect With a Mentor ConsultantCommented:
PHP isn't a strongly-typed language. In .NET, anytime you declare a variable you have to tell the system what type of variable it is (for the most part) so that the code knows how to run before it actually runs.

In PHP, the code is "figured out" as it runs. So if you try to use a variable as an object:

$x->doSomething();

...then when PHP finds this line, it looks at the current value of $x and says, "Is $x an object? If it is, does it have a method called doSomething? If so, run it!" It doesn't matter that you didn't explicitly tell PHP that $x is an object.

If $x was a number or a string, then PHP would throw an error when it got to that line, but as long as it's an object, then it will work.
0
 
Dan CraciunConnect With a Mentor IT ConsultantCommented:
foreach ($this->_employees as $name_them)

This means that $name_them is an object from the collection $this->_employees.

HTH,
Dan
0
 
aboo_sConnect With a Mentor Commented:
$this->_employees = $e
 foreach ($this->_employees as $name_them)
this means $name_them is of type Employee
Which is apparently an object
0
 
Ray PaseurConnect With a Mentor Commented:
I do not understand how a non object can call methods
This can only work if the methods are public methods.  If the methods are protected methods, only the object instance of the class and extensions of the class can call the methods.  If the methods are private, only the class can call the method.
0
 
rgb192Author Commented:
thanks all.

Now i understand 'strongly typed'
0
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.