Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

how is rewind method called

<!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;
        echo "<p>{$e->getName()} has been added to the {$this->_name} department.</p>";
    }

    // 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');

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

// Add the employees to the department:
$hr->addEmployee($e1);
$hr->addEmployee($e2);

// 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

output:
Jane Doe has been added to the Human Resources department.

John Doe has been added to the Human Resources department.

Department Employees
Jane Doe

John Doe




from Larry Ulman php object oriented book.

No other code is required

Implements iterator
I do not understand how rewind method is called.
Goes from line87 to line52

User generated image
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

There is nothing special about how rewind() is called -- it's just a class method (also known as a "member function").  Iterator is an interface that requires the user, among other things, to create the rewind() method.

This is how you call it:

$x = new Department('Department-Name-You-Choose');
/* DO SOMETHING */
$x->rewind();

Note line 28 in the constructor.  This does the same thing that the rewind() method does.  Line 28 could also say:

$this->rewind();
Avatar of rgb192

ASKER

so
line 28
 $this->_position = 0;
calls rewind method?



note: rewind method was called
Sheesh -- for some reason my posts seem to be getting thrown away by EE.

line 28
 $this->_position = 0;
calls rewind method?
No, that does the same thing as the rewind() method but it does not call the rewind() method.  I would not write duplicate code like that, but there is probably no real harm in praxis.

As to why the IDE says the rewind() method was called -- we cannot say from what we see here.  There is no code showing a call to the rewind() method.  So we can't tell you where the IDE got that idea, sorry.
Avatar of rgb192

ASKER

so in the screenshot, nusphere ide thinks that rewind method is called when property is set to 0

rewind method purpose is to set property to 0
But I do not think vise versa because there are many different ways to set property to 0 and just because set, does not mean that rewind method was called

so you think that nusphere debugger is making an error?
Avatar of rgb192

ASKER

am I not understanding?

should I edit code to test rewind method?
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

// Required by Iterator; returns the position to the first spot:
    function rewind() {
        $this->_position = 0;
        error_log('Rewind() method called');
        echo '<h2>rewind was called</h2>';
    }

I added echo also.
no output even though debugger went through every line

Thanks for making code easier to understand.