Solved

non object calling methods

Posted on 2014-03-10
5
275 Views
Last Modified: 2014-03-11
<!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;
        foreach ($this->_employees as $name_them){
          echo '<br>name: '.$name_them->getName();
        }
        echo "<p>{$e->getName()} has been added to the {$this->_name} department.</p>";
    }
    function __toString(){
      return $var;
    }

    // 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');
$film = new Department('last_time');
// Create employees:
$e1 = new Employee('Jane Doe');
$e2 = new Employee('John Doe');
$e3 = new Employee('amy');
// Add the employees to the department:
$hr->addEmployee($e1);
$hr->addEmployee($e2);
$hr->addEmployee($e3);
$film->addEmployee($e1);
// 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


code works but
I do not understand how a non object can call methods

line 33, line 35

or maybe just line 33

$name_them was not declared as an object
maybe $e is an object
0
Comment
Question by:rgb192
5 Comments
 
LVL 34

Assisted Solution

by:Dan Craciun
Dan Craciun earned 125 total points
ID: 39917811
foreach ($this->_employees as $name_them)

This means that $name_them is an object from the collection $this->_employees.

HTH,
Dan
0
 
LVL 10

Assisted Solution

by:aboo_s
aboo_s earned 125 total points
ID: 39917816
$this->_employees = $e
 foreach ($this->_employees as $name_them)
this means $name_them is of type Employee
Which is apparently an object
0
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 125 total points
ID: 39918096
PHP isn't a strongly-typed language. In .NET, anytime you declare a variable you have to tell the system what type of variable it is (for the most part) so that the code knows how to run before it actually runs.

In PHP, the code is "figured out" as it runs. So if you try to use a variable as an object:

$x->doSomething();

...then when PHP finds this line, it looks at the current value of $x and says, "Is $x an object? If it is, does it have a method called doSomething? If so, run it!" It doesn't matter that you didn't explicitly tell PHP that $x is an object.

If $x was a number or a string, then PHP would throw an error when it got to that line, but as long as it's an object, then it will work.
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 125 total points
ID: 39918412
I do not understand how a non object can call methods
This can only work if the methods are public methods.  If the methods are protected methods, only the object instance of the class and extensions of the class can call the methods.  If the methods are private, only the class can call the method.
0
 

Author Closing Comment

by:rgb192
ID: 39920677
thanks all.

Now i understand 'strongly typed'
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

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 …
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

706 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

19 Experts available now in Live!

Get 1:1 Help Now