3v4l.org

run code in 300+ PHP versions simultaneously
<?php // --- 1. シンプルな独自定義クラス --- class MySimpleClass { public string $publicProp; private int $privateProp; // privateプロパティもシリアライズされる protected array $protectedProp; public ?MySimpleClass $parent = null; // 自己参照または他のオブジェクト参照用 public array $nestedArrayData; public string $sharedStringRef1; public string $sharedStringRef2; public object $sharedObjectRef1; public object $sharedObjectRef2; public object $customSerializableObj; public object $userRoleEnum; // Backed Enum public object $statusEnum; // Pure Enum public $nullValue; public bool $booleanTrue; public float $floatValue; public int $integerValue; public string $japaneseString; public function __construct(string $public, int $private, array $protected) { $this->publicProp = $public; $this->privateProp = $private; $this->protectedProp = $protected; $this->nestedArrayData = []; // 初期化 } public function getPrivateProp(): int { return $this->privateProp; } } // --- 2. カスタムシリアライズ可能なクラス (PHP 7.4+ の __serialize/__unserialize) --- class MyCustomSerializable { public string $dataString; private int $dataNumber; public function __construct(string $s, int $n) { $this->dataString = $s; $this->dataNumber = $n; } // オブジェクトがシリアライズされる直前に呼び出される // シリアライズしたいプロパティを連想配列で返す public function __serialize(): array { echo "__serialize() called for MyCustomSerializable\n"; return [ 's' => $this->dataString, 'n' => $this->dataNumber, ]; } // オブジェクトがデシリアライズされた直後に呼び出される // __serialize() で返された連想配列を受け取る public function __unserialize(array $data): void { echo "__unserialize() called for MyCustomSerializable\n"; $this->dataString = $data['s']; $this->dataNumber = $data['n']; } public function getCustomData(): string { return "Custom: " . $this->dataString . " / " . $this->dataNumber; } } // --- 3. Enum の定義 (PHP 8.1+) --- enum UserRole: string // Backed Enum { case Admin = 'admin'; case Editor = 'editor'; case Viewer = 'viewer'; } enum Status // Pure Enum { case Active; case Inactive; } // --- シリアライズ対象データ構造の構築 (トップレベルがオブジェクト) --- // オブジェクト参照と値参照のための共通データ $commonObject = new MySimpleClass('共通オブジェクト', 500, ['shared' => true]); $commonString = "共有される文字列データ"; // トップレベルのオブジェクトを作成し、多様なデータをプロパティとして持つ $topLevelObject = new MySimpleClass('Top Level Object', 999, ['root_data' => 'initial']); // 1. スカラー型 (Scalar Types) $topLevelObject->japaneseString = "これは日本語の文字列です"; $topLevelObject->integerValue = 123; $topLevelObject->floatValue = 45.67; $topLevelObject->booleanTrue = true; $topLevelObject->nullValue = null; // 2. リスト (数値キーの配列) $topLevelObject->nestedArrayData = [ 'Item A', 'Item B', 'Item C', 10, false, ]; // 3. 連想配列 (Associative Array) $topLevelObject->protectedProp = [ // protectedProp を再利用して連想配列を入れる 'assoc_key1' => 'assoc_value1', 'assoc_key2' => 789, 'deep_nested_array' => [ 'sub_key_x' => 'sub_value_x', 'sub_key_y' => 12.34, ], ]; // 4. オブジェクト参照 (r:) - 別のオブジェクトへの参照 $topLevelObject->sharedObjectRef1 = $commonObject; $topLevelObject->sharedObjectRef2 = $commonObject; // 同じオブジェクトへの参照 // 5. 値参照 (R:) - 同じ文字列への参照 $topLevelObject->sharedStringRef1 = $commonString; $topLevelObject->sharedStringRef2 = $commonString; // 同じ文字列への参照 // 6. Enum (PHP 8.1+ の場合) $topLevelObject->userRoleEnum = UserRole::Editor; $topLevelObject->statusEnum = Status::Active; // 7. カスタムシリアライズ可能なオブジェクト (C:) $topLevelObject->customSerializableObj = new MyCustomSerializable('オブジェクト内のカスタム', 777); // --- シリアライズ実行 --- $serializedResult = serialize($topLevelObject); // --- 出力 --- echo "--- 元データ (var_export) ---\n"; var_export($topLevelObject); echo "\n\n"; echo "--- シリアライズ結果 ---\n"; echo $serializedResult; echo "\n\n"; ?>

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.00.0130.00820.13
8.4.150.0010.00014.05
8.4.140.0150.00918.08
8.4.130.0080.01218.18
8.4.120.0110.00820.67
8.4.110.0150.00518.88
8.4.100.0120.00818.08
8.4.90.0140.00717.76
8.4.80.0170.00517.90
8.4.70.0280.00318.00
8.4.60.0350.00817.94
8.4.50.0420.00817.79
8.4.40.0400.00917.53
8.4.30.0400.01117.33
8.4.20.0440.01217.81
8.4.10.0270.00317.99
8.3.280.0010.00214.05
8.3.270.0120.00916.95
8.3.260.0110.00916.93
8.3.250.0110.00719.16
8.3.240.0040.00716.70
8.3.230.0120.00816.81
8.3.220.0350.00616.97
8.3.210.0380.00716.97
8.3.200.0420.00616.97
8.3.190.0320.00716.97
8.3.180.0210.00216.97
8.3.170.0170.00516.97
8.3.160.0180.00617.00
8.3.150.0200.00316.97
8.3.140.0240.00516.97
8.3.130.0380.00916.97
8.3.120.0190.00516.97
8.3.110.0160.00416.97
8.3.100.0300.00716.97
8.3.90.0570.00616.97
8.3.80.0410.01116.97
8.3.70.0390.01316.97
8.3.60.0400.01116.97
8.3.50.0430.00516.97
8.3.40.0330.00617.73
8.3.30.0190.00117.82
8.3.20.0310.00817.77
8.3.10.0360.00817.56
8.3.00.0170.01017.63
8.2.290.0140.00518.63
8.2.280.0240.00316.97
8.2.270.0370.00816.97
8.2.260.0380.00716.97
8.2.250.0360.00816.97
8.2.240.0320.01016.97
8.2.230.0370.00616.97
8.2.220.0350.00816.97
8.2.210.0370.00616.97
8.2.200.0360.00716.97
8.2.190.0290.00416.97
8.2.180.0170.00916.97
8.2.170.0410.01117.87
8.2.160.0390.00717.75
8.2.150.0340.00917.64
8.2.140.0220.00517.65
8.2.130.0200.00317.54
8.2.120.0220.00917.45
8.2.110.0310.01017.58
8.2.100.0390.00617.46
8.2.90.0230.00717.66
8.2.80.0310.00717.50
8.2.70.0360.00517.64
8.2.60.0310.01217.51
8.2.50.0350.01117.41
8.2.40.0180.00117.67
8.2.30.0170.00417.66
8.2.20.0140.00517.57
8.2.10.0150.00317.72
8.2.00.0160.00317.80

preferences:
38.19 ms | 403 KiB | 5 Q