使用对象运算符循环

Looping with Object Operators

我正在研究 PHP Calendar (Corey Worrell),并且有一个关于实例循环的问题。为了初始化日历,我必须输出这个:

$calendar->standard('today')
        ->standard('prev-next')
        ->standard('holidays')
        ->attach($event1)
        ->attach($event2)
        ->attach($event3)
        ->attach($event4)
        ->attach($event5)
        ->attach($event6)
        ->attach($event7)
        ->attach($event8)
        ->attach($event9)
        ->attach($event10)
        ->attach($event11)
        ->attach($event12)
        ->attach($event13)
        ->attach($event14)
        ->attach($event15)
        ->attach($event16)
        ->attach($event17);

每个 ->attach($event#) 在日历上输出一个事件。我想遍历这些数字递增的事件名称,但是在该代码中的任何位置添加 for 循环会破坏所有内容,输出此错误:

PHP Catchable fatal error: Argument 1 passed to Event_Subject::attach() must be an instance of Event_Observer, null given, called in /calendar/index.php on line 75 and defined in /calendar/classes/event_subject.php on line 21

这是我试过的循环:

$calendar->standard('today')
        ->standard('prev-next')
        ->standard('holidays')
        for ($inc = 0; $inc <= $number_of_events; $inc++) {
            if ($inc == $number_of_events) {
                ->attach($$event_name);
            }
            else {
                ->attach($$event_name)
            }
        }

如何在此处循环?我的事件存储在 MySQL 中,我正在执行 $number_of_events = $result->num_rows 以确定返回的事件数。 ->attach($event#) 会循环,重复直到总 $number_of_events 被命中。

这就是所谓的方法链。 class returns 的每个方法都通过 $this 调用对象的实例,允许您堆叠方法调用。方法链接是可能的,因为函数 returns 对您的对象的引用。

因为 class 中的每个函数都支持方法链 returns 调用对象,您可以将返回的对象重新分配回原始 $calander 变量;

 for ($inc = 0; $inc <= $number_of_events; $inc++) {
        if ($inc == $number_of_events) {
            $calander = $calander->attach($event1);
        }
        else {
            $calander = $calander->attach($event1);
        }
    }

此外,如果您想遍历变量名,可以在循环中使用变量 variable;

$variable = "event".$inc;

$calander = $calander->attach($$variable);

所以这将变成 $event0, $event1$event2

循环的问题是->运算符前面什么都没有。 -> 引用了一个对象的 属性,但您没有为它提供对象。您可以通过将 $calendar 放在孤独运算符 ($calendar->...) 的前面来解决它,但它仍然不是非常漂亮的代码。

我建议改为:

我认为您可以在循环中一个接一个地添加事件我假设您已经在使用它来创建 $event1$event2 等。我不知道您是什么用于从数据库中获取数据或您的 table 结构是什么样的,但我将提供 MySQLi 的示例。应该很容易修改其他替代方案。

//Add the standards.
$calendar->standard('today')
    ->standard('prev-next')
    ->standard('holidays');

//Connect to the database here and query for the events using MySQLi.

//Loop through the results.
while($row = $result->fetch_assoc()) {
    //Create an event from the database row...
    $event = calendar->event()
         ->condition('timestamp', $row['TIMESTAMP'])
         ->title('Hello All', $row['TITLE'])
         ->output('My Custom Event', $row['OUTPUT']);
    //...and attach it.
    $calendar->attach($event);
}

这不是直接复制粘贴的代码,更多的是关于如何组织它的建议。

此外,为了将来,您不应命名变量 $name1$name2 等,然后使用 $$ 来引用它们。请改用 arrays

这是解决方案。不知道为什么将其更改为引用 $calendar 每行都有效,但确实如此。感谢大家。

$calendar->standard('today');
$calendar->standard('prev-next');
$calendar->standard('holidays');

for ($x = 1; $x <= $number_of_events; $x++) {
    $event_name = "event".$x;
    $calendar->attach($$event_name);
}