もくじ
後置インクリメント
$num = 0; echo $num++; // 0 echo $num++; // 1
インクリメントする前に変数を応える。
前置インクリメント
$num =0; echo ++$num; // 1 echo ++$num; // 2
変数をインクリメントしてから応える。
使用例
PHPデザインパターン入門P168
<?php
require_once 'Command.class.php';
class Queue
{
private $commands;
private $current_index;
public function __construct() {
$this->commands = array();
$this->current_index = 0;
}
public function addCommand(Command $command) {
$this->commands[] = $command;
}
public function run() {
while(!is_null($command = $this->next())) {
$command->execute();
}
}
public function next() {
if(count($this->commands) === 0 ||
count($this->commands) <= $this->current_index) {
return null;
} else {
return $this->commands[$this->current_index++];
}
}
}
ここで
return $this->commands[$this->current_index++];
これは
$this->commands[0]を初回返す
public function run() {
while(!is_null($command = $this->next())) {
$command->execute();
}
}
初回の$command->execute()は、
$commands[0]->execute()と同じ。
![[DDD] Value Object 値オブジェクト](https://www.yuulinux.tokyo/contents/wp-content/uploads/2020/01/PHP-logo_2-150x150.jpeg)





![Nginx [error] no live upstreams while connecting to upstream, client: xxx.xxx.xxx.xxx, server](https://www.yuulinux.tokyo/contents/wp-content/uploads/2022/04/Nginx_logo.svg_-150x150.png)