willium
willium

Reputation: 2128

Multiple Calls To Function - Cannot redeclare class - php

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?

Upvotes: 1

Views: 2492

Answers (5)

dev-null-dweller
dev-null-dweller

Reputation: 29462

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.

Upvotes: 3

Lee
Lee

Reputation: 13542

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).

Upvotes: 1

Pekka
Pekka

Reputation: 449515

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.

Upvotes: 1

Mchl
Mchl

Reputation: 62387

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.

Upvotes: 1

Brad Mace
Brad Mace

Reputation: 27886

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

Upvotes: 1

Related Questions