Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

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

callstack: Goes from line87 to line52
0
rgb192
Asked:
rgb192
  • 4
  • 3
1 Solution
 
Ray PaseurCommented:
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();
0
 
rgb192Author Commented:
so
line 28
 $this->_position = 0;
calls rewind method?



note: rewind method was called
0
 
Ray PaseurCommented:
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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
rgb192Author Commented:
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?
0
 
rgb192Author Commented:
am I not understanding?

should I edit code to test rewind method?
0
 
Ray PaseurCommented:
I don't know the NuSphere debugger, so I can't really comment on its output.  But I can tell you that every layer of complex interactions introduces risk that the combined software systems will not work as expected.  I prefer to create the tests manually.  One of the tools I use is error_log().  You can drop a function call into the program logic wherever you need it and when the script has been run you can examine the logs to see what got logged.  You might choose something like this -- see line 53:

<!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;
        error_log('Rewind() method called');
    }

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

0
 
rgb192Author Commented:
// 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.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now