Solved

how is rewind method called

Posted on 2014-03-31
7
173 Views
Last Modified: 2014-04-28
<!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
Comment
Question by:rgb192
  • 4
  • 3
7 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39967755
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
 

Author Comment

by:rgb192
ID: 39967814
so
line 28
 $this->_position = 0;
calls rewind method?



note: rewind method was called
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39969234
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:rgb192
ID: 39970840
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
 

Author Comment

by:rgb192
ID: 40019116
am I not understanding?

should I edit code to test rewind method?
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40020062
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
 

Author Closing Comment

by:rgb192
ID: 40028769
// 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

828 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