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"; ?>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XPrbP
function name:  (null)
number of ops:  62
compiled vars:  !0 = $commonObject, !1 = $commonString, !2 = $topLevelObject, !3 = $serializedResult
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   77     0  E >   DECLARE_CLASS                                            'userrole'
   84     1        DECLARE_CLASS                                            'status'
   93     2        NEW                                              $4      'MySimpleClass'
          3        SEND_VAL_EX                                              '%E5%85%B1%E9%80%9A%E3%82%AA%E3%83%96%E3%82%B8%E3%82%A7%E3%82%AF%E3%83%88'
          4        SEND_VAL_EX                                              500
          5        SEND_VAL_EX                                              <array>
          6        DO_FCALL                                      0          
          7        ASSIGN                                                   !0, $4
   94     8        ASSIGN                                                   !1, '%E5%85%B1%E6%9C%89%E3%81%95%E3%82%8C%E3%82%8B%E6%96%87%E5%AD%97%E5%88%97%E3%83%87%E3%83%BC%E3%82%BF'
   97     9        NEW                                              $8      'MySimpleClass'
         10        SEND_VAL_EX                                              'Top+Level+Object'
         11        SEND_VAL_EX                                              999
         12        SEND_VAL_EX                                              <array>
         13        DO_FCALL                                      0          
         14        ASSIGN                                                   !2, $8
  100    15        ASSIGN_OBJ                                               !2, 'japaneseString'
         16        OP_DATA                                                  '%E3%81%93%E3%82%8C%E3%81%AF%E6%97%A5%E6%9C%AC%E8%AA%9E%E3%81%AE%E6%96%87%E5%AD%97%E5%88%97%E3%81%A7%E3%81%99'
  101    17        ASSIGN_OBJ                                               !2, 'integerValue'
         18        OP_DATA                                                  123
  102    19        ASSIGN_OBJ                                               !2, 'floatValue'
         20        OP_DATA                                                  45.67
  103    21        ASSIGN_OBJ                                               !2, 'booleanTrue'
         22        OP_DATA                                                  <true>
  104    23        ASSIGN_OBJ                                               !2, 'nullValue'
         24        OP_DATA                                                  null
  107    25        ASSIGN_OBJ                                               !2, 'nestedArrayData'
  108    26        OP_DATA                                                  <array>
  116    27        ASSIGN_OBJ                                               !2, 'protectedProp'
  117    28        OP_DATA                                                  <array>
  126    29        ASSIGN_OBJ                                               !2, 'sharedObjectRef1'
         30        OP_DATA                                                  !0
  127    31        ASSIGN_OBJ                                               !2, 'sharedObjectRef2'
         32        OP_DATA                                                  !0
  130    33        ASSIGN_OBJ                                               !2, 'sharedStringRef1'
         34        OP_DATA                                                  !1
  131    35        ASSIGN_OBJ                                               !2, 'sharedStringRef2'
         36        OP_DATA                                                  !1
  134    37        FETCH_CLASS_CONSTANT                             ~23     'UserRole', 'Editor'
         38        ASSIGN_OBJ                                               !2, 'userRoleEnum'
         39        OP_DATA                                                  ~23
  135    40        FETCH_CLASS_CONSTANT                             ~25     'Status', 'Active'
         41        ASSIGN_OBJ                                               !2, 'statusEnum'
         42        OP_DATA                                                  ~25
  138    43        NEW                                              $27     'MyCustomSerializable'
         44        SEND_VAL_EX                                              '%E3%82%AA%E3%83%96%E3%82%B8%E3%82%A7%E3%82%AF%E3%83%88%E5%86%85%E3%81%AE%E3%82%AB%E3%82%B9%E3%82%BF%E3%83%A0'
         45        SEND_VAL_EX                                              777
         46        DO_FCALL                                      0          
         47        ASSIGN_OBJ                                               !2, 'customSerializableObj'
         48        OP_DATA                                                  $27
  142    49        INIT_FCALL                                               'serialize'
         50        SEND_VAR                                                 !2
         51        DO_ICALL                                         $29     
         52        ASSIGN                                                   !3, $29
  145    53        ECHO                                                     '---+%E5%85%83%E3%83%87%E3%83%BC%E3%82%BF+%28var_export%29+---%0A'
  146    54        INIT_FCALL                                               'var_export'
         55        SEND_VAR                                                 !2
         56        DO_ICALL                                                 
  147    57        ECHO                                                     '%0A%0A'
  149    58        ECHO                                                     '---+%E3%82%B7%E3%83%AA%E3%82%A2%E3%83%A9%E3%82%A4%E3%82%BA%E7%B5%90%E6%9E%9C+---%0A'
  150    59        ECHO                                                     !3
  151    60        ECHO                                                     '%0A%0A'
  154    61      > RETURN                                                   1

Class MySimpleClass:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XPrbP
function name:  __construct
number of ops:  12
compiled vars:  !0 = $public, !1 = $private, !2 = $protected
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   26     3        ASSIGN_OBJ                                               'publicProp'
          4        OP_DATA                                                  !0
   27     5        ASSIGN_OBJ                                               'privateProp'
          6        OP_DATA                                                  !1
   28     7        ASSIGN_OBJ                                               'protectedProp'
          8        OP_DATA                                                  !2
   29     9        ASSIGN_OBJ                                               'nestedArrayData'
         10        OP_DATA                                                  <array>
   30    11      > RETURN                                                   null

End of function __construct

Function getprivateprop:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XPrbP
function name:  getPrivateProp
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   FETCH_OBJ_R                                      ~0      'privateProp'
          1        VERIFY_RETURN_TYPE                                       ~0
          2      > RETURN                                                   ~0
   35     3*       VERIFY_RETURN_TYPE                                       
          4*     > RETURN                                                   null

End of function getprivateprop

End of class MySimpleClass.

Class MyCustomSerializable:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XPrbP
function name:  __construct
number of ops:  7
compiled vars:  !0 = $s, !1 = $n
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   46     2        ASSIGN_OBJ                                               'dataString'
          3        OP_DATA                                                  !0
   47     4        ASSIGN_OBJ                                               'dataNumber'
          5        OP_DATA                                                  !1
   48     6      > RETURN                                                   null

End of function __construct

Function __serialize:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XPrbP
function name:  __serialize
number of ops:  9
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   54     0  E >   ECHO                                                     '__serialize%28%29+called+for+MyCustomSerializable%0A'
   56     1        FETCH_OBJ_R                                      ~0      'dataString'
          2        INIT_ARRAY                                       ~1      ~0, 's'
   57     3        FETCH_OBJ_R                                      ~2      'dataNumber'
          4        ADD_ARRAY_ELEMENT                                ~1      ~2, 'n'
          5        VERIFY_RETURN_TYPE                                       ~1
          6      > RETURN                                                   ~1
   59     7*       VERIFY_RETURN_TYPE                                       
          8*     > RETURN                                                   null

End of function __serialize

Function __unserialize:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XPrbP
function name:  __unserialize
number of ops:  9
compiled vars:  !0 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   63     0  E >   RECV                                             !0      
   65     1        ECHO                                                     '__unserialize%28%29+called+for+MyCustomSerializable%0A'
   66     2        FETCH_DIM_R                                      ~2      !0, 's'
          3        ASSIGN_OBJ                                               'dataString'
          4        OP_DATA                                                  ~2
   67     5        FETCH_DIM_R                                      ~4      !0, 'n'
          6        ASSIGN_OBJ                                               'dataNumber'
          7        OP_DATA                                                  ~4
   68     8      > RETURN                                                   null

End of function __unserialize

Function getcustomdata:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XPrbP
function name:  getCustomData
number of ops:  9
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   72     0  E >   FETCH_OBJ_R                                      ~0      'dataString'
          1        CONCAT                                           ~1      'Custom%3A+', ~0
          2        CONCAT                                           ~2      ~1, '+%2F+'
          3        FETCH_OBJ_R                                      ~3      'dataNumber'
          4        CONCAT                                           ~4      ~2, ~3
          5        VERIFY_RETURN_TYPE                                       ~4
          6      > RETURN                                                   ~4
   73     7*       VERIFY_RETURN_TYPE                                       
          8*     > RETURN                                                   null

End of function getcustomdata

End of class MyCustomSerializable.

Class UserRole: [no user functions]
Class Status: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
156.66 ms | 1024 KiB | 15 Q