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"; ?>
Output for 8.2.0 - 8.2.29, 8.3.0 - 8.3.27, 8.4.1 - 8.4.14, 8.5.0
Fatal error: Uncaught Error: Cannot access protected property MySimpleClass::$protectedProp in /in/XPrbP:116 Stack trace: #0 {main} thrown in /in/XPrbP on line 116
Process exited with code 255.
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.
Output for 8.3.28
/bin/php-8.3.28: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.3.28) /bin/php-8.3.28: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.3.28) /bin/php-8.3.28: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.3.28) /bin/php-8.3.28: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.3.28)
Process exited with code 1.

preferences:
67.39 ms | 407 KiB | 5 Q