3v4l.org

run code in 300+ PHP versions simultaneously
<?php class CookieStorage { public function addCookie(Cookie $cookie): void { $encodedCookieValue = $this->encodeCookie($cookie->value); setcookie( $cookie->name, $encodedCookieValue, $cookie->options ); $_COOKIE[$cookie->name] = $encodedCookieValue; } public function getCookie(string $cookieName): ?Cookie { $encodedCookieValue = $_COOKIE[$cookieName] ?? null; if (null === $encodedCookieValue) { return null; } $cookieValue = $this->decodeCookie($encodedCookieValue); return new Cookie($cookieName, $cookieValue); } private function encodeCookie(mixed $value): string { return json_encode($value); } private function decodeCookie(string $value): mixed { return json_decode($value); } } class Cookie { readonly public string $name; readonly public mixed $value; readonly public array $options; public function __construct(string $name, mixed $value, array $options = []) { $this->name = $name; $this->value = $value; if ([] === $options) { $options = [ 'expires' => time() + 60 * 60 * 24 * 30, 'secure' => true, 'httponly' => true, ]; } $this->options = $options; } } $storage = new CookieStorage(); $cookie = new Cookie('test', ['one', 'two']); $storage->addCookie($cookie); var_dump($storage->getCookie('test'));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/D2RQi
function name:  (null)
number of ops:  18
compiled vars:  !0 = $storage, !1 = $cookie
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   58     0  E >   NEW                                              $2      'CookieStorage'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $2
   59     3        NEW                                              $5      'Cookie'
          4        SEND_VAL_EX                                              'test'
          5        SEND_VAL_EX                                              <array>
          6        DO_FCALL                                      0          
          7        ASSIGN                                                   !1, $5
   60     8        INIT_METHOD_CALL                                         !0, 'addCookie'
          9        SEND_VAR_EX                                              !1
         10        DO_FCALL                                      0          
   61    11        INIT_FCALL                                               'var_dump'
         12        INIT_METHOD_CALL                                         !0, 'getCookie'
         13        SEND_VAL_EX                                              'test'
         14        DO_FCALL                                      0  $9      
         15        SEND_VAR                                                 $9
         16        DO_ICALL                                                 
         17      > RETURN                                                   1

Class CookieStorage:
Function addcookie:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/D2RQi
function name:  addCookie
number of ops:  19
compiled vars:  !0 = $cookie, !1 = $encodedCookieValue
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   RECV                                             !0      
    5     1        INIT_METHOD_CALL                                         'encodeCookie'
          2        CHECK_FUNC_ARG                                           
          3        FETCH_OBJ_FUNC_ARG                               $2      !0, 'value'
          4        SEND_FUNC_ARG                                            $2
          5        DO_FCALL                                      0  $3      
          6        ASSIGN                                                   !1, $3
    7     7        INIT_FCALL                                               'setcookie'
    8     8        FETCH_OBJ_R                                      ~5      !0, 'name'
          9        SEND_VAL                                                 ~5
    9    10        SEND_VAR                                                 !1
   10    11        FETCH_OBJ_R                                      ~6      !0, 'options'
         12        SEND_VAL                                                 ~6
    7    13        DO_ICALL                                                 
   12    14        FETCH_OBJ_R                                      ~9      !0, 'name'
         15        FETCH_W                      global              $8      '_COOKIE'
         16        ASSIGN_DIM                                               $8, ~9
         17        OP_DATA                                                  !1
   13    18      > RETURN                                                   null

End of function addcookie

Function getcookie:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 9
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/D2RQi
function name:  getCookie
number of ops:  21
compiled vars:  !0 = $cookieName, !1 = $encodedCookieValue, !2 = $cookieValue
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
   16     1        FETCH_IS                                         ~3      '_COOKIE'
          2        FETCH_DIM_IS                                     ~4      ~3, !0
          3        COALESCE                                         ~5      ~4
          4        QM_ASSIGN                                        ~5      null
          5        ASSIGN                                                   !1, ~5
   18     6        TYPE_CHECK                                    2          !1
          7      > JMPZ                                                     ~7, ->9
   19     8    > > RETURN                                                   null
   22     9    >   INIT_METHOD_CALL                                         'decodeCookie'
         10        SEND_VAR_EX                                              !1
         11        DO_FCALL                                      0  $8      
         12        ASSIGN                                                   !2, $8
   24    13        NEW                                              $10     'Cookie'
         14        SEND_VAR_EX                                              !0
         15        SEND_VAR_EX                                              !2
         16        DO_FCALL                                      0          
         17        VERIFY_RETURN_TYPE                                       $10
         18      > RETURN                                                   $10
   25    19*       VERIFY_RETURN_TYPE                                       
         20*     > RETURN                                                   null

End of function getcookie

Function encodecookie:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/D2RQi
function name:  encodeCookie
number of ops:  8
compiled vars:  !0 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   RECV                                             !0      
   28     1        INIT_FCALL                                               'json_encode'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $1      
          4        VERIFY_RETURN_TYPE                                       $1
          5      > RETURN                                                   $1
   29     6*       VERIFY_RETURN_TYPE                                       
          7*     > RETURN                                                   null

End of function encodecookie

Function decodecookie:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/D2RQi
function name:  decodeCookie
number of ops:  7
compiled vars:  !0 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   RECV                                             !0      
   32     1        INIT_FCALL                                               'json_decode'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $1      
          4      > RETURN                                                   $1
   33     5*       VERIFY_RETURN_TYPE                                       
          6*     > RETURN                                                   null

End of function decodecookie

End of class CookieStorage.

Class Cookie:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 16
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
filename:       /in/D2RQi
function name:  __construct
number of ops:  19
compiled vars:  !0 = $name, !1 = $value, !2 = $options
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      <array>
   43     3        ASSIGN_OBJ                                               'name'
          4        OP_DATA                                                  !0
   44     5        ASSIGN_OBJ                                               'value'
          6        OP_DATA                                                  !1
   46     7        IS_IDENTICAL                                             !2, <array>
          8      > JMPZ                                                     ~5, ->16
   48     9    >   INIT_FCALL                                               'time'
         10        DO_ICALL                                         $6      
         11        ADD                                              ~7      $6, 2592000
         12        INIT_ARRAY                                       ~8      ~7, 'expires'
         13        ADD_ARRAY_ELEMENT                                ~8      <true>, 'secure'
         14        ADD_ARRAY_ELEMENT                                ~8      <true>, 'httponly'
   47    15        ASSIGN                                                   !2, ~8
   54    16    >   ASSIGN_OBJ                                               'options'
         17        OP_DATA                                                  !2
   55    18      > RETURN                                                   null

End of function __construct

End of class Cookie.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
138.02 ms | 1011 KiB | 18 Q