?
Solved

how is rewind method called

Posted on 2014-03-31
7
Medium Priority
?
192 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 111

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 111

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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 dynamically set the form action using jQuery.
Suggested Courses

719 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