3v4l.org

run code in 300+ PHP versions simultaneously
<?php class CommentInfoController { private $pdo; public function __construct() { $this->pdo = Database::getInstance('db1'); }   public function updateCommentInfo(array $new_comment_info): array { try { $this->pdo->beginTransaction(); // 更新対象id $comment_id = $new_comment_info['comment_id']; /* ステップ1 現状のコメント情報を取得 */ // comments テーブルのSELECT $commentMapper = new CommentMapper(); $comment = new Comment(); $args = ['comment_id'=>$comment_id]; $now_comment_info = $commentMapper->findCommentInfos($args)[0]; if(!$now_comment_info) { throw new InvalidArgumentException('comment_id:' . $comment_id . 'は存在しません'); } /* var_export($now_comment_info); array ( 'comment_id' => 1, 'content' => 'content1', 'tags' => array ( 0 => array ( 'tag_name' => 'bbb', 'tag_kind_id' => 2, ), ), ) */ /* ステップ2 現状のコメント情報($now_comment_info) と 新しいコメント情報($new_comment_info) の 差分を抽出して更新 */ // content がある場合 if(!empty($new_comment_info['content'])) { if ($now_comment_info['content'] !== $new_comment_info['content']) { // comments テーブルのUPDATE $commentMapper = new CommentMapper(); $comment = new Comment(); $comment->id = $comment_id; $comment->content = $new_comment_info['content']; $commentMapper->update($comment); } } // tags がある場合 if(!empty($new_comment_info['tags'])) { // tags の差分を得る $tmp = $this->compareTags($now_comment_info['tags'], $new_comment_info['tags']); $added_tags = $tmp['added']; $removed_tags = $tmp['removed']; /* var_export($tmp); array ( 'removed' => array ( 0 => array ( 'tag_name' => 'aaa', 'tag_kind_id' => 1, ), ), 'added' => array ( 0 => array ( 'tag_name' => 'bbb', 'tag_kind_id' => 2, ), ), ) */ // $added_tag がDBになければ $tags にセットし bulkInsert で保存 $tags = []; $tagMapper = new TagMapper(); foreach ($added_tags as $added_tag) { $sth = $tagMapper->find($added_tag); $row = $sth->fetch(); if (!$row) { $tag = new Tag(); $tag->fromArray($added_tag); // 配列を行クラスにセット $tags[] = $tag; // 行クラスを追加 } } if(!empty($tags)) { /* var_export($tags); array ( 0 => \Tag::__set_state(array( 'data' => array ( 'tag_name' => 'bbb', 'tag_kind_id' => 2, ), )), ) */ $tagMapper->bulkInsert($tags); } // commentHasTags のINSERT $commentHasTags = []; foreach ($tags as $data) { $commentHasTag = new CommentHasTag(); $commentHasTag->comment_id = $comment_id; $commentHasTag->tag_id = $data->id; $commentHasTags[] = $commentHasTag; } /* var_export($commentHasTags); array ( 0 => \CommentHasTag::__set_state(array( 'data' => array ( 'comment_id' => 1, 'tag_id' => 54, ), )), ) */ $commentHasTagMapper = new CommentHasTagMapper(); $commentHasTagMapper->bulkInsert($commentHasTags); // $removed_tags のリレーションを削除 $commentHasTagMapper = new CommentHasTagMapper(); // ★こういうときって宣言すんのか? foreach ($removed_tags as $removed_tag) { $sth = $tagMapper->find($removed_tag); $row = $sth->fetch(); $conditions = ['comment_id'=>$comment_id, 'tag_id'=>$row->id]; $commentHasTagMapper->delete($conditions); } } $this->pdo->commit(); return $new_comment_info; } catch (Exception $e) { $this->pdo->rollBack(); log($e->getMessage()); } } }
Output for git.master_jit, git.master
Parse error: syntax error, unexpected identifier " ", expecting "function" in /in/DqCAN on line 11
Process exited with code 255.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
164.53 ms | 1002 KiB | 7 Q