3v4l.org

run code in 300+ PHP versions simultaneously
<?php /*-------------------------------------------------------- クラス --------------------------------------------------------*/ // fetch, fetchAll で実行される abstract class DataModel { const BOOLEAN = 'boolean' , INTEGER = 'integer' , DOUBLE = 'double' , FLOAT = 'double' , STRING = 'string' , DATETIME = 'dateTime'; const BIND_TYPE = array( 'boolean' => PDO::PARAM_BOOL, 'integer' => PDO::PARAM_INT, 'double' => PDO::PARAM_STR, 'string' => PDO::PARAM_STR, 'dateTime' => PDO::PARAM_STR, ); protected array $data = array(); // 拡張クラスで宣言される protected static string $primaryKeyName; protected static array $omit = array(); protected static array $schema = array(); function __get($prop) { if (property_exists($this, $prop)) { return static::$$prop; } elseif (isset($this->data[$prop])) { return $this->data[$prop]; } elseif (isset(static::$schema[$prop])) { return null; } else { throw new InvalidArgumentException($prop . 'は存在しません'); } } function __isset($prop) { return isset($this->data[$prop]); } /** * @throws Exception */ function __set($prop, $val) { if (!isset(static::$schema[$prop])) { throw new InvalidArgumentException($prop . 'はセットできません'); } $schema = static::$schema[$prop]; if ($schema === static::DATETIME) { if ($val instanceof DateTime) { $this->data[$prop] = $val; } else { $this->data[$prop] = new DateTime($val); } return $this->data[$prop]; } else { return match ($schema) { static::BOOLEAN => $this->data[$prop] = (bool)$val, static::INTEGER => $this->data[$prop] = (int)$val, static::DOUBLE => $this->data[$prop] = (double)$val, default => $this->data[$prop] = (string)$val, }; } } function toArray(): array { return $this->data; } /** * @throws Exception */ function fromArray(array $arr): void { foreach ($arr as $key => $val) { $this->__set($key, $val); } } abstract function isValid(string $save_type); // TIMESTAMP のオブジェクトを探し DATETIME の値に変換する public function convertTimestampToDatetime(){ $result = $this; $data = []; foreach ($this->data as $key => $value) { if ($value instanceof DateTime) { $data[$key] = $value->format('Y-m-d H:i:s'); } else { $data[$key] = $value; } } $result->data = $data; return $result; } } abstract class DataMapper { const MODEL_CLASS = ''; const TABLE_NAME = ''; protected PDO $pdo; function __construct() { // 拡張クラスで宣言される値を確認 if (!class_exists(static::MODEL_CLASS)) { throw new InvalidArgumentException('MODEL_CLASSが存在しません'); } assert(!empty(static::TABLE_NAME), 'TABLE_NAMEが存在しません'); // pdo if (empty($pdo)) { $pdo = new PDO ('mysql:host=host.docker.internal;dbname=test', 'test', 'pass'); } $this->pdo = $pdo; } protected function decorate(PDOStatement $stmt) { $stmt->setFetchMode(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE, static::MODEL_CLASS); return $stmt; } public function insert(DataModel $data, array $omit = []) { // isValid $valided = $data->isValid('insert'); if ($valided !== true) { throw new InvalidArgumentException($valided.'の値が不正です'); } $schema = $data->schema; $pk = $data->primaryKeyName; $omitCols = array_merge($omit, $data->omit); $cols = array_diff(array_keys($schema), array_merge(array($pk), $omitCols)); $colsClause = implode(',', $cols); $valsClause = rtrim(str_repeat('?,', count($cols)), ','); $sql = 'INSERT INTO ' . static::TABLE_NAME . ' (' . $colsClause . ')' . ' VALUES(' . $valsClause . ')'; echo '<h4>【$sql】</h4>'; var_dump($sql); $sth = $this->pdo->prepare($sql); $this->decorate($sth); $n = 1; foreach($cols as $col) { $val = $data->$col; $schemaType = $schema[$col]; if ($schemaType == DataModel::DATETIME) { /** @var DateTime $val */ $val = $val->format('Y-m-d H:i:s'); } $sth->bindValue($n++, $val, DataModel::BIND_TYPE[$schemaType]); } $sth->execute(); $data->$pk = $this->pdo->lastInsertId(); // var_dump($omitCols); // 'author' を含んでいる foreach($omitCols as $omitCol) { unset($data->$omitCol); // 削除されない } } } class Entry extends DataModel { protected static string $primaryKeyName = 'entryId'; protected static array $omit = array('created_at', 'updated_at'); // CREATE TABLE でのデフォ値を使用 protected static array $schema = array( 'entryId' => parent::INTEGER , 'author' => parent::STRING , 'title' => parent::STRING , 'content' => parent::STRING , 'published' => parent::DATETIME ); function isValid(string $save_type) { // authorは100文字まで、insertで必須 $val = $this->author; if ( ($save_type === 'insert' && empty($val)) || (!mb_check_encoding($val) || mb_strlen($val) > 100) ) { return 'author'; } // titleは100文字まで、必須 $val = $this->title; if (empty($val) || !mb_check_encoding($val) || mb_strlen($val) > 100) { return 'title'; } // contentは10000字まで、必須 $val = $this->content; if (empty($val) || !mb_check_encoding($val) || mb_strlen($val) > 10000) { return 'content'; } // publishedは型があっていれば問題ない $val = $this->published; if (empty($val)) { return 'published'; } return true; } } class EntryMapper extends DataMapper { const MODEL_CLASS = 'Entry'; const TABLE_NAME = 'entries'; } /*-------------------------------------------------------- 実行 --------------------------------------------------------*/ $entry = new Entry(); $entryMapper = new EntryMapper(); $new_info = [ 'author' => '太郎', 'title' => '今日の天気', 'content' => '雨です', 'published' => new DateTime ]; $entry->fromArray($new_info); $entryMapper->insert($entry, ['author']); echo '<h4>【$entry】</h4>'; var_export($entry); echo '<h4>【$entryArr】</h4>'; $entryArr = $entry->convertTimestampToDatetime()->toArray(); var_export($entryArr);

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.5.10.0110.00619.68
8.5.00.0140.00820.14
8.4.150.0020.00014.05
8.4.140.0140.00717.89
8.4.130.0100.01117.80
8.4.120.0090.01224.27
8.4.110.0040.00518.80
8.4.100.0160.00719.06
8.4.90.0090.01220.67
8.4.80.0120.00717.97
8.4.70.0080.00617.95
8.4.60.0130.00620.49
8.4.50.0110.01217.98
8.4.40.0130.00720.74
8.4.30.0100.01022.29
8.4.20.0040.00418.12
8.4.10.0130.00722.27
8.3.280.0120.00818.65
8.3.270.0110.00916.54
8.3.260.0100.00616.54
8.3.250.0130.00716.69
8.3.240.0090.01017.32
8.3.230.0140.00518.43
8.3.220.0120.00819.17
8.3.210.0110.00416.99
8.3.200.0030.00616.69
8.3.190.0140.00517.34
8.3.180.0060.00218.16
8.3.170.0120.00318.93
8.3.160.0070.01316.82
8.3.150.0040.00416.85
8.3.140.0090.00918.89
8.3.130.0000.00818.35
8.3.120.0040.00419.24
8.3.110.0080.00020.94
8.3.100.0080.00024.06
8.3.90.0080.00026.77
8.3.80.0060.00317.97
8.3.70.0120.00416.50
8.3.60.0040.01116.61
8.3.50.0090.01218.48
8.3.40.0110.00420.25
8.3.30.0130.00318.76
8.3.20.0080.00024.18
8.3.10.0030.00524.66
8.3.00.0030.00526.16
8.2.290.0090.00720.72
8.2.280.0070.00218.51
8.2.270.0130.00917.46
8.2.260.0090.00018.48
8.2.250.0060.00318.68
8.2.240.0040.00418.84
8.2.230.0030.00622.58
8.2.220.0030.00537.54
8.2.210.0090.00626.77
8.2.200.0070.00318.42
8.2.190.0140.00716.88
8.2.180.0070.01316.88
8.2.170.0110.00719.06
8.2.160.0070.00722.96
8.2.150.0080.00025.66
8.2.140.0040.00424.66
8.2.130.0040.00426.16
8.2.120.0040.00419.77
8.2.110.0100.00020.89
8.2.100.0060.00618.16
8.2.90.0000.00918.00
8.2.80.0040.00419.03
8.2.70.0040.00417.75
8.2.60.0200.00017.46
8.2.50.0070.01117.11
8.2.40.0130.00317.75
8.2.30.0130.00317.50
8.2.20.0050.00517.63
8.2.10.0080.00817.50
8.2.00.0080.00817.63
8.1.330.0110.01022.04
8.1.320.0140.00716.53
8.1.310.0110.00418.41
8.1.300.0050.00318.04
8.1.290.0060.00330.84
8.1.280.0160.00025.92
8.1.270.0140.00424.66
8.1.260.0040.00426.35
8.1.250.0040.00428.09
8.1.240.0060.00322.13
8.1.230.0080.00421.10
8.1.220.0030.00518.60
8.1.210.0090.00018.77
8.1.200.0000.00917.38
8.1.190.0090.00017.47
8.1.180.0140.00317.25
8.1.170.0120.00517.25
8.1.160.0140.00317.13
8.1.150.0030.01417.13
8.1.140.0000.01117.47
8.1.130.0130.00417.22
8.1.120.0120.00617.13
8.1.110.0060.01317.00
8.1.100.0070.01117.46
8.1.90.0110.00517.38
8.1.80.0130.00417.11
8.1.70.0080.00817.38
8.1.60.0090.00917.63
8.1.50.0100.00717.34
8.1.40.0100.00817.50
8.1.30.0140.00317.63
8.1.20.0180.00017.38
8.1.10.0110.00817.47
8.1.00.0170.00017.25
8.0.300.0030.00719.93
8.0.290.0000.00816.88
8.0.280.0120.00516.75
8.0.270.0140.00316.52
8.0.260.0120.00416.63
8.0.250.0100.00716.75
8.0.240.0120.00616.87
8.0.230.0140.00416.88
8.0.220.0170.00316.52
8.0.210.0100.00716.88
8.0.200.0090.00916.61
8.0.190.0080.00816.63
8.0.180.0170.00016.75
8.0.170.0170.00016.63
8.0.160.0160.00016.63
8.0.150.0170.00016.52
8.0.140.0080.00816.63
8.0.130.0150.00316.88
8.0.120.0100.00716.63
8.0.110.0100.00716.75
8.0.100.0060.01016.63
8.0.90.0060.01216.88
8.0.80.0080.00816.88
8.0.70.0090.00916.88
8.0.60.0170.00016.63
8.0.50.0140.00216.63
8.0.30.0060.01016.75
8.0.20.0120.00416.75
8.0.10.0110.00616.75

preferences:
45.06 ms | 403 KiB | 5 Q