tags:

views:

29

answers:

5

I'm currently learning php, and I'm testing out oop and classes.

My code is:

<?php 
require('user.php');
$user = new User($username);
$projects = $user->getProjects();

for ($n = 0; $n<count($projects); $n++) {
    echo "<li>";
    echo "<a id=\"menu_pages\" href=\"\"><img src=\"../images/types/project.png\"/>" . $projects[$n]->name . "</a>";

    echo "<ul>";
    $items = $user->getItems($n);
    for ($i = 0; $i<count($items); $i++) {
        echo "<li><a href=\"../\"><img src=\"../images/types/" . $items[$i]->type . ".png\"/>" . $items[$i]->name . "</a></li>";
    }
    echo "</ul>";
    echo "</li>"; 

}
?>

and I'm getting the error: Fatal error: Cannot redeclare class Item in /home/ios/public_html/classes/item.php on line 2

I found that its because I'm using

$items = $user->getItems($n);

in a loop. Because if I change it to

$items = $user->getItems(1);

it works. I cannot really figure out anyway of getting around the error.

Help please :)

Thanks in advance.

The methods:

    function getItems($parent,$type = NULL) {
    $items = array();
    $username = $this->uname;

    $userid = $this->getId($username);

    include('classes/config.php');


    if ($type != NULL) {
    }

    require_once('classes/item.php');

    foreach ($pdo->query($sql) as $row) {
        $instance = new Item();
        $instance->name = $row['name'];
        $instance->id = $row['id'];
        $instance->type = $row['type'];
        $instance->parent_id = $row['parent_id'];

        $items[] = $instance;
        unset($instance);
    }

function getProjects() {
    $projects = array();
    $username = $this->uname;

    $userid = $this->getId($username);

    include('classes/config.php');


    require_once('classes/project.php');

    foreach ($pdo->query($sql) as $row) {
        $proj = new Project();
        $proj->name = $row['name'];
        $proj->id = $row['id'];

        $projects[] = $proj;
        unset($proj);
    }

    return $projects;  

}

edit:

its still not working with changing it. The code goes through with no errors, but now what should be under the first is under the second and the first is blank. See any issues in my logic?

+1  A: 

change it to require_once('user.php'); and it won't try to execute user.php on each pass.

bemace
edited the post, its still not working with changing it. The code goes through with no errors, but now what should be under the first is under the second and the first is blank. See any issues in my logic?
tap touch click
+1  A: 

It's hard to say withut seeing what's in getItems() function, but it is possible that it includes a file where Item class is defined in a loop, which triggers this error. Try using require_once() instead.

Mchl
posted the code.
tap touch click
+1  A: 

The error message points to a duplicate class definition (The blueprint that starts with Class classname).

If you are pointing at the correct line of code - I'm not competely convinced you are - I'm pretty sure that $user->getItems() includes a PHP file, in which the class is redefined.

The immediate problem can be fixed using require_once() or include_once() but you should probably change your getItems function so that the class definition is loaded elsewhere in the first place.

Pekka
+1  A: 

I'll guess that the getItems() function on the User class probably has a line something like:

require('item.php');

if you change that to:

require_once('item.php');

you should be good to go.

(if I'm wrong about that, then please post the code for the getItems() function, so we can diagnose further).

Lee
I posted the code. Thanks!
tap touch click
yes, and there's the line I said to look for: `require('classes/item.php');`. Just change that to `require_once('classes/item.php');`. Did you actually look for it yourself (as per my suggestion) before posting it?
Lee
I did, but I wanted to post the code quick. I'm getting a Parse error: syntax error, unexpected ';', expecting T_FUNCTION in /home/ios/public_html/classes/user.php on line 89 (last line of the class which is: ?>)
tap touch click
+1  A: 

Use require_once while including files with classes definition (I guess this will be somewhere in getItems()) or organize your files and define magic function __autoload() that will load it for you when used.

After looking into provided source, I doubt that this works for more than one row...

Here you create instance of Item before even checking if there are any items, and after first loop $instance is destroyed:

require('classes/item.php');
            $instance = new Item();

foreach ($pdo->query($sql) as $row) {
    $instance->name = $row['name'];
    $instance->id = $row['id'];
    $instance->type = $row['type'];
    $instance->parent_id = $row['parent_id'];

    $items[] = $instance;
    unset($instance);
}

This should look like:

require_once('classes/item.php');

foreach ($pdo->query($sql) as $row) {
    $instance = new Item();
    $instance->name = $row['name'];
    $instance->id = $row['id'];
    $instance->type = $row['type'];
    $instance->parent_id = $row['parent_id'];

    $items[] = $instance;
    unset($instance);
}

So for every item new instance is created.

dev-null-dweller
edited the post. its still not working with changing it. The code goes through with no errors, but now what should be under the first is under the second and the first is blank. See any issues in my logic?
tap touch click
Not sure what `first` and `second` is but see edited answer - I corrected your `getItems()` method, maybe now it'll work as expected
dev-null-dweller
I added your changes. I'm getting a Parse error: syntax error, unexpected ';', expecting T_FUNCTION in /home/ios/public_html/classes/user.php on line 89 (last line of the class which is: ?>)
tap touch click
what I mean by "first" and "second" is the array returned by getProjects has two objects. In the for loop you will notice it is nested, and for some reason, the values in the db associated with the "first" project, and are echo'd for the "second" the first is left blank. I will post a new question with this if I cannot resolve it myself.
tap touch click
I fixed the missing ;, that was careless of me. Just the 'first' and 'second' issue remains. I'll mess around with it until I can get it working.
tap touch click
To fix `first` and `second` issue, you may want to call `getItems` with `$projects[$n]->id` instead of `$n`
dev-null-dweller