Solved

many methods are required by iterator but where is iterator? If does not exist, please create.

Posted on 2014-03-09
4
265 Views
Last Modified: 2014-03-11
This is from Larry Ulman php tutorial.
<!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 Iterator1 {
    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


when I add a '1' to line 18

Fatal error: Interface 'Iterator1' not found in C:\wamp\www\phpvqp3_scripts\ch08\iterator.php on line 18

But I can not find Iterator
0
Comment
Question by:rgb192
  • 2
4 Comments
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
Comment Utility
You may be working with the SPL here?  Not sure about that.  Please see the man page here:
http://php.net/manual/en/language.oop5.interfaces.php

An interface is similar to a class definition, so if you change the name in one place, you must change the name in all places, including the definition.

Here is a teaching example showing how an interface is defined and a class is used to implement the interface.  The interface is just a statement of what a class must do.  It provides the names of the methods and "signature" of the method arguments, but it defines no code for the methods -- that part is up to the author of the class that implements the interface.  It's pretty easy to recognize this sort of thing from the code.  Notice that there is no control structure (curly braces) after the function statements - they end in a semicolon.

<?php // demo/oop_interface.php
error_reporting(E_ALL);
echo "<pre>";

// AREA FORMULAE: http://www.mathsisfun.com/area.html
define('PI', 3.1416);

// SEE http://php.net/manual/en/language.oop5.interfaces.php
interface Shapes
{
    public function rectangle($w, $h);
    public function triangle($b, $h);
    public function ellipse($a, $b);
}

class Geometry implements Shapes
{
    protected $shapes = array();

    public function rectangle($w, $h)
    {
        $shape["n"] = __FUNCTION__;
        $shape["w"] = $w;
        $shape["h"] = $h;
        $shape["area"]  = $w * $h;
        $this->shapes[] = $shape;
    }

    public function triangle($b, $h)
    {
        $shape["n"] = __FUNCTION__;
        $shape["b"] = $b;
        $shape["h"] = $h;
        $shape["area"]  = 0.5 * $b * $h;
        $this->shapes[] = $shape;
    }

    public function ellipse($a, $b)
    {
        $shape["n"] = __FUNCTION__;
        $shape["a"] = $a;
        $shape["b"] = $b;
        $shape["area"]  = PI * $a * $b;
        $this->shapes[] = $shape;
    }
}

class Area extends Geometry
{
    public function getTotalArea()
    {
        $total_area = 0.0;
        foreach($this->shapes as $shape)
        {
            $total_area += $shape['area'];
        }
        return $total_area;
    }
}

// CREATE AN INSTANCE OF THE OBJECT
$x = new Area;

// SHOW THE NEWLY INSTANTIATED OBJECT
var_dump($x);

// CALL SOME METHODS AND SHOW THE OBJECT
$x->rectangle(3,5);
$x->rectangle(2,2);
var_dump($x);

// CALL SOME MORE METHODS
$x->triangle(5,3);
$x->ellipse(1,4);

// SHOW THE OBJECT AGAIN AFTER SOME MORE WORK
var_dump($x);
echo $x->gettotalArea();

Open in new window

0
 

Author Comment

by:rgb192
Comment Utility
I have renamed the file and run, and moved to another directory and run so file is not including other files


and I understand your example with interface and classes implementing need these methods.  Class Geometry would need rectangle, triangle, ellipse

interface Shapes
{
    public function rectangle($w, $h);
    public function triangle($b, $h);
    public function ellipse($a, $b);
}

class Geometry implements Shapes


but where is it written that iterator classes require methods
current
key
next
rewind
valid


because I do not understand how these methods work and would like to see man page

is there an spl man page for these methods
0
 
LVL 33

Accepted Solution

by:
Slick812 earned 250 total points
Comment Utility
greetings rgb192, there is some sort of "mixup" here, for some unknown reason your tutorial ads a  "1" so it is called as "Iterator1" , I do not think this is correct and should be "Iterator" , try  Iterator instead, and see if that works. - There is a very useful Iterator interface in the PHP, look at the manual page here -
http://php.net/manual/en/class.iterator.php

in there example they call it like this -
class myIterator implements Iterator {

IMPORTANT (at least to me) the php SPL interfaces ARE VERY USEFUL ! BUT you must understand what the interface meaning is about, the definition (explanation) for interface, given by the php manual is not going to be useful, as I understand and use "interface" in my coding, an interface has functioning "BEYOND" the Class definition - - - - as all of the SPL interfaces, have functioning that is below the class definition in the PHP system usage as the Iterator interface can be used for the php system "foreach loop" call as your tutorial does here -

// Loop through the department:
echo "<h2>Department Employees</h2>";
foreach ($hr as $e) {  // IMPORTANT, the php SYSTEM does the foreach loop,
                               //  NOT the "Class Object"
                              // so the next() method is a php system call "needed in foreach"
    echo "<p>{$e->getName()}</p>";
}

you can NOT call foreach( ) as an object method -
$hr->foreach() { // WILL NOT WORK

I good-great thing about SPL interfaces, is that you can do anything that you need to do in the next( ) method (or other required methods), or any other of the required method, to get extremely Unique functioning for the output of a foreach( ) loop in this case.

I have never been able to find a good reason to make an interface in PHP -
interface Order {
public function makeOrder($dept, $price);
}

since in PHP you can not define any special "System" or Class-Object functioning that is Beyond the interface code. I just make a parent class instead, and extend that.
The SPL interfaces have been a big help for me to have multipurpose Classes that do extra functioning in the PHP system. I especially like the -  ArrayAccess interface  -
0
 

Author Closing Comment

by:rgb192
Comment Utility
okay so implements and http://php.net/manual/en/class.iterator.php

thanks.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

762 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

6 Experts available now in Live!

Get 1:1 Help Now