【问题标题】:PHP objects and instancesPHP 对象和实例
【发布时间】:2014-07-18 14:30:01
【问题描述】:

我会尽可能简单地分解它,然后发布所有必要的代码。

目标:为所有用户获取一个周期内每个子周期的所有答案,没有答案的我将标记单元格(不要担心这部分) 所以我可以稍后对其进行数据外推。

用户类:用户有一个 Id 和一个句点列表

Period 类:Period 有一个 Id 和一个子周期列表

Sub Period 类: Sub Period 有一个 Id 和一个问题列表

问题类:每个问题都有答案

现在是代码,前几个嵌套循环中的注释行是首先弄清楚的关键。到目前为止,每个实例的第二行都是我的尝试。在未注释这两行的情况下,我在这一行得到一个错误: $user->periods[$period]->addSubPeriod($period->periodId, $subPeriod);

说: 注意:未定义的偏移量:1致命错误:在非对象上调用成员函数 addSubPeriod()

$periods_arr = array(1, 2, 3, 4, 5);
$subPeriods_arr = array(1, 2);
$questionslist = array("q_1_1", "q_1_2", "q_2_1", "q_2_2", "q_3_1", "q_4_1", "q_5_1");

class User {
    public $userId;
    public $periods = array();

    public function __construct($number) {
        $this->userId = $number;
    }

    public function addPeriod($id, $pno) 
    {
        $this->periods[$id] = new Period($pno);
    }
}

class Period {
    public $periodId;
    public $subPeriods = array();

    public function __construct($number)
    {
        $this->periodId = $number;
    }

    public function addSubPeriod($id, $spno)
    {
        $this->subPeriods[$id] = new SubPeriod($spno);
    }
}

class SubPeriod {
    public $subPeriodId;
    public $answers = array();

    public function __construct($number)
    {
        $this->subPeriodId = $number;
    }

    public function addAnswer($id, $answer)
    {
        $this->answers[$id] = new Question($answer);
    }
}

class Question {
    public $answer;

    public function __construct($ans)
    {
        $this->answer = $ans;
    }

    public function getAnswer()
    {
        echo $this->answer; 
    }
}        

$userlist = array();

$sql = 'SELECT user_ref FROM _survey_1_as GROUP BY user_ref ORDER BY user_ref ASC';
$result = mysql_query($sql);

while ($row = mysql_fetch_array($result))
{
    array_push($userlist, new User($row['user_ref']));
}

foreach ($userlist as &$user)
{
    foreach ($periods_arr as &$period)
    {
        // set the current users ($user) period
        // $user->addPeriod($user->userId, $period);

        foreach ($subPeriods_arr as &$subPeriod)
        {
            // set the sub_period for the current users period
            // $user->periods[$period]->addSubPeriod($period->periodId, $subPeriod);

            foreach($questionslist as &$aquestion)
            {
                $sql = 'SELECT ' . $aquestion . ' FROM _survey_1_as WHERE user_ref = ' .
                    $user->userId . ' AND answer_period = ' . $period . ' AND answer_sub_period = ' . 
                    $subPeriod .''; 

                $result = mysql_query($sql);

                while ($row = mysql_fetch_array($result))
                {
                    $user->periods[$period]->subPeriods[$subPeriod]->addAnswer($row[$aquestion]);       
                }
            }
        }
    }   
}

【问题讨论】:

  • 已整理完毕,将尽快发布答案。过去的帖子中的每个人都将一个简单的问题复杂化了大声笑

标签: php arrays oop instance-variables


【解决方案1】:

错误信息很多。您的数组没有索引为 1 的元素(未定义偏移量:1),因此尝试使用它与尝试执行 $x = null; $x->doSomething(); 相同,这当然会导致 致命错误:调用到非对象上的成员函数...

您可能想var_dump($user->periods) 并仔细调试您的代码。

【讨论】:

  • 等待 3 分钟,由于某种原因,我现在完全没有出错,但有最后一个问题。要编辑原始代码/帖子,然后我会在这里发表评论
  • 同时停止使用 & 进行迭代,这可能会导致奇怪的问题,而且据我所知,您的场景中根本不需要它。
  • 是的,我最近看到了 & 标志。无论如何,我已经弄清楚了,并且效果很好。我将在 8 小时内发布答案,因为我还不能发布。
猜你喜欢
  • 2016-01-18
  • 2015-02-22
  • 2011-01-10
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 2016-06-19
  • 1970-01-01
  • 2013-03-14
相关资源
最近更新 更多