0

我的代码有问题。我有这样的代码:

<?php
     include('php/SelectHistory.php');
     include('php/SelectSmallHistoryUser.php');
     include('php/SelectSmallHistoryProject.php');
     include('php/SelectSmallHistoryFunctionality.php');

     $newHistoryRow = SelectHistory();

     echo "<table width='100%'>";

     if (count($newHistoryRow) > 0)
     {
         foreach ($newHistoryRow as $current)
         {
              $chosenUser = SelectSmallHistoryUser($current->userID);
              $chosenProject = SelectSmallHistoryProject($current->projectID);
              $chosenFunctionality = SelectSmallHistoryFunctionality($current->functionalityID);
              echo "<tr>";
              echo "<td>" . $chosenUser->fullName . " was busy with " . $chosenFunctionality->functionalityName . " on " . $chosenProject->projectName . " at " . $current->lastModifiedDate;
              echo "</tr>";
              unset($chosenUser);
              unset($chosenProject);
              unset($chosenFunctionality);
          }
      }
      else
      {
          echo "<tr><td>No History To Display.</td></tr>";
      }

      echo "</table>";
?>

我遇到的问题是在循环中,它声明了一个驻留在类中的方法。现在因为它正在处理来自数据库的数据,如果事物的数量超过一个,我会收到“已声明的类”错误。

有没有办法可以解决这个问题,或者我可以使用另一种方法吗?

4

4 回答 4

1

我猜是做一个新的。$var = new SelectHistory();

于 2013-02-11T10:52:41.527 回答
1

而不是像上面那样创建对象,你应该做的是定义一个静态函数来获取你想要的数据。您可以在不创建对象的情况下调用如下函数。

SelectSmallHistoryUser::getData($current->userID);

使用循环创建对象是不可接受的。阅读更多

于 2013-02-11T11:06:48.300 回答
1

这是创建类的正确方法:

class TestClass {
    public $property;
    //some other properties
    function __construct($id) {
        $this->property=$id;
        //do some other stuff
    }
    //some other functions
}

创建类实例的正确方法是:

 $test = new TestClass($id);

注意__construct()函数和new关键字,然后再次尝试执行您的脚本。

于 2013-02-11T11:06:50.517 回答
0

在循环中声明或包含类是不好的。但是检查一下:http ://www.php.net/manual/en/function.class-exists.php

于 2013-02-11T11:03:35.130 回答