Solved

how is rewind method called

Posted on 2014-03-31
7
161 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 108

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 108

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 108

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now