3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * A simple user account database */ class UserAccountDatabase { private $data = []; /** * Save a user account * * @param string $username * @param string $password * @return void * @throws Exception */ public function saveAccount($username, $password) { if ($this->getAccount($username) !== null) { throw new \Exception('Username already exists in database.'); } $this->data[$username] = $password; } /** * Get a user account by username * * @param string $username * @return array|null */ public function getAccount($username) { return isset($this->data[$username]) ? $this->data[$username] : null; } /** * Delete a user account by username * * @param string $username * @return void */ public function deleteAccount($username) { unset($this->data[$username]); } } /** * You must implement this interface */ interface UserAccountManagerInterface { /** * @param UserAccountDatabase $db */ public function __construct(UserAccountDatabase $db); /** * Allow a user to create an account. Should return true if account creation * was successful and false otherwise. * * @param string $username * @param string $password * @return bool */ public function create($username, $password); /** * Allow a user to authenticate using the same username and password that * they used when creating their account. Should return true if the account * exists and the password matches or false otherwise. * * @param string $username * @param string $password * @return bool */ public function authenticate($username, $password); } class UserAccountManager implements UserAccountManagerInterface { private $db; public function __construct(UserAccountDatabase $db) { $this->db = $db; } public function create($username, $password) { try { $this->db->saveAccount($username, $password); return true; } catch (Exception $e) { return false; } } public function authenticate($username, $password) { $stored_pass = $this->db->getAccount($username); if ($stored_pass === null) { return false; } return $password === $stored_pass; } } /////////////////////////// $input = fopen("php://memory", "rw"); $data = '[ [ ["abe","12345"], ["ben","67890"], ["ben","67890"] ], [ ["abe","12345"], ["ben","67890"], ["ben","67890"], ["carla","12345"] ] ]'; fwrite($input, $data); fseek($input, 0); // Setup Streams //$input = fopen("php://stdin", "r"); $output = fopen("php://stdout","w"); // Input (JSON) $arguments = json_decode(trim(stream_get_contents($input)), true, 16); //var_dump($arguments); die(); $db = new UserAccountDatabase(); $manager = new UserAccountManager($db); $result = []; // Create Accounts foreach ($arguments[0] as $account) { $result[0][] = call_user_func_array([$manager, 'create'], $account); } // Authenticate Accounts foreach ($arguments[1] as $account) { $result[1][] = call_user_func_array([$manager, 'authenticate'], $account); } // Output (JSON) fwrite($output, json_encode($result)); fclose($output);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 42, Position 2 = 53
Branch analysis from position: 42
2 jumps found. (Code = 78) Position 1 = 43, Position 2 = 53
Branch analysis from position: 43
1 jumps found. (Code = 42) Position 1 = 42
Branch analysis from position: 42
Branch analysis from position: 53
2 jumps found. (Code = 77) Position 1 = 56, Position 2 = 67
Branch analysis from position: 56
2 jumps found. (Code = 78) Position 1 = 57, Position 2 = 67
Branch analysis from position: 57
1 jumps found. (Code = 42) Position 1 = 56
Branch analysis from position: 56
Branch analysis from position: 67
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 67
Branch analysis from position: 53
filename:       /in/3jOjl
function name:  (null)
number of ops:  79
compiled vars:  !0 = $input, !1 = $data, !2 = $output, !3 = $arguments, !4 = $db, !5 = $manager, !6 = $result, !7 = $account
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   82     0  E >   DECLARE_CLASS                                            'useraccountmanager'
  115     1        INIT_FCALL                                               'fopen'
          2        SEND_VAL                                                 'php%3A%2F%2Fmemory'
          3        SEND_VAL                                                 'rw'
          4        DO_ICALL                                         $8      
          5        ASSIGN                                                   !0, $8
  116     6        ASSIGN                                                   !1, '%5B%0A++++%5B%0A++++++++%5B%22abe%22%2C%2212345%22%5D%2C%0A++++++++%5B%22ben%22%2C%2267890%22%5D%2C%0A++++++++%5B%22ben%22%2C%2267890%22%5D%0A++++%5D%2C%0A++++%5B%0A++++++++%5B%22abe%22%2C%2212345%22%5D%2C%0A++++++++%5B%22ben%22%2C%2267890%22%5D%2C%0A++++++++%5B%22ben%22%2C%2267890%22%5D%2C%0A++++++++%5B%22carla%22%2C%2212345%22%5D%0A++++%5D%0A%5D'
  129     7        INIT_FCALL                                               'fwrite'
          8        SEND_VAR                                                 !0
          9        SEND_VAR                                                 !1
         10        DO_ICALL                                                 
  130    11        INIT_FCALL                                               'fseek'
         12        SEND_VAR                                                 !0
         13        SEND_VAL                                                 0
         14        DO_ICALL                                                 
  134    15        INIT_FCALL                                               'fopen'
         16        SEND_VAL                                                 'php%3A%2F%2Fstdout'
         17        SEND_VAL                                                 'w'
         18        DO_ICALL                                         $13     
         19        ASSIGN                                                   !2, $13
  137    20        INIT_FCALL                                               'json_decode'
         21        INIT_FCALL                                               'trim'
         22        INIT_FCALL                                               'stream_get_contents'
         23        SEND_VAR                                                 !0
         24        DO_ICALL                                         $15     
         25        SEND_VAR                                                 $15
         26        DO_ICALL                                         $16     
         27        SEND_VAR                                                 $16
         28        SEND_VAL                                                 <true>
         29        SEND_VAL                                                 16
         30        DO_ICALL                                         $17     
         31        ASSIGN                                                   !3, $17
  141    32        NEW                                              $19     'UserAccountDatabase'
         33        DO_FCALL                                      0          
         34        ASSIGN                                                   !4, $19
  142    35        NEW                                              $22     'UserAccountManager'
         36        SEND_VAR_EX                                              !4
         37        DO_FCALL                                      0          
         38        ASSIGN                                                   !5, $22
  143    39        ASSIGN                                                   !6, <array>
  146    40        FETCH_DIM_R                                      ~26     !3, 0
         41      > FE_RESET_R                                       $27     ~26, ->53
         42    > > FE_FETCH_R                                               $27, !7, ->53
  147    43    >   INIT_ARRAY                                       ~30     !5
         44        ADD_ARRAY_ELEMENT                                ~30     'create'
         45        INIT_USER_CALL                                0          'call_user_func_array', ~30
         46        SEND_ARRAY                                               !7
         47        CHECK_UNDEF_ARGS                                         
         48        DO_FCALL                                      0  $31     
         49        FETCH_DIM_W                                      $28     !6, 0
         50        ASSIGN_DIM                                               $28
         51        OP_DATA                                                  $31
  146    52      > JMP                                                      ->42
         53    >   FE_FREE                                                  $27
  151    54        FETCH_DIM_R                                      ~32     !3, 1
         55      > FE_RESET_R                                       $33     ~32, ->67
         56    > > FE_FETCH_R                                               $33, !7, ->67
  152    57    >   INIT_ARRAY                                       ~36     !5
         58        ADD_ARRAY_ELEMENT                                ~36     'authenticate'
         59        INIT_USER_CALL                                0          'call_user_func_array', ~36
         60        SEND_ARRAY                                               !7
         61        CHECK_UNDEF_ARGS                                         
         62        DO_FCALL                                      0  $37     
         63        FETCH_DIM_W                                      $34     !6, 1
         64        ASSIGN_DIM                                               $34
         65        OP_DATA                                                  $37
  151    66      > JMP                                                      ->56
         67    >   FE_FREE                                                  $33
  156    68        INIT_FCALL                                               'fwrite'
         69        SEND_VAR                                                 !2
         70        INIT_FCALL                                               'json_encode'
         71        SEND_VAR                                                 !6
         72        DO_ICALL                                         $38     
         73        SEND_VAR                                                 $38
         74        DO_ICALL                                                 
  157    75        INIT_FCALL                                               'fclose'
         76        SEND_VAR                                                 !2
         77        DO_ICALL                                                 
         78      > RETURN                                                   1

Class UserAccountDatabase:
Function saveaccount:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 11
Branch analysis from position: 7
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3jOjl
function name:  saveAccount
number of ops:  15
compiled vars:  !0 = $username, !1 = $password
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   20     2        INIT_METHOD_CALL                                         'getAccount'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0  $2      
          5        TYPE_CHECK                                  1020          $2
          6      > JMPZ                                                     ~3, ->11
   21     7    >   NEW                                              $4      'Exception'
          8        SEND_VAL_EX                                              'Username+already+exists+in+database.'
          9        DO_FCALL                                      0          
         10      > THROW                                         0          $4
   24    11    >   FETCH_OBJ_W                                      $6      'data'
         12        ASSIGN_DIM                                               $6, !0
         13        OP_DATA                                                  !1
   25    14      > RETURN                                                   null

End of function saveaccount

Function getaccount:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 8
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3jOjl
function name:  getAccount
number of ops:  11
compiled vars:  !0 = $username
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
   35     1        FETCH_OBJ_IS                                     ~1      'data'
          2        ISSET_ISEMPTY_DIM_OBJ                         0          ~1, !0
          3      > JMPZ                                                     ~2, ->8
          4    >   FETCH_OBJ_R                                      ~3      'data'
          5        FETCH_DIM_R                                      ~4      ~3, !0
          6        QM_ASSIGN                                        ~5      ~4
          7      > JMP                                                      ->9
          8    >   QM_ASSIGN                                        ~5      null
          9    > > RETURN                                                   ~5
   36    10*     > RETURN                                                   null

End of function getaccount

Function deleteaccount:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3jOjl
function name:  deleteAccount
number of ops:  4
compiled vars:  !0 = $username
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   RECV                                             !0      
   46     1        FETCH_OBJ_UNSET                                  $1      'data'
          2        UNSET_DIM                                                $1, !0
   47     3      > RETURN                                                   null

End of function deleteaccount

End of class UserAccountDatabase.

Class UserAccountManagerInterface:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3jOjl
function name:  __construct
number of ops:  2
compiled vars:  !0 = $db
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   58     0  E >   RECV                                             !0      
          1      > RETURN                                                   null

End of function __construct

Function create:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3jOjl
function name:  create
number of ops:  3
compiled vars:  !0 = $username, !1 = $password
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   68     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2      > RETURN                                                   null

End of function create

Function authenticate:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3jOjl
function name:  authenticate
number of ops:  3
compiled vars:  !0 = $username, !1 = $password
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   79     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2      > RETURN                                                   null

End of function authenticate

End of class UserAccountManagerInterface.

Class UserAccountManager:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3jOjl
function name:  __construct
number of ops:  4
compiled vars:  !0 = $db
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   86     0  E >   RECV                                             !0      
   88     1        ASSIGN_OBJ                                               'db'
          2        OP_DATA                                                  !0
   89     3      > RETURN                                                   null

End of function __construct

Function create:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
Found catch point at position: 9
Branch analysis from position: 9
2 jumps found. (Code = 107) Position 1 = 10, Position 2 = -2
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3jOjl
function name:  create
number of ops:  12
compiled vars:  !0 = $username, !1 = $password, !2 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   91     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   94     2        FETCH_OBJ_R                                      ~3      'db'
          3        INIT_METHOD_CALL                                         ~3, 'saveAccount'
          4        SEND_VAR_EX                                              !0
          5        SEND_VAR_EX                                              !1
          6        DO_FCALL                                      0          
   95     7      > RETURN                                                   <true>
          8*       JMP                                                      ->11
   96     9  E > > CATCH                                       last         'Exception'
   97    10    > > RETURN                                                   <false>
   99    11*     > RETURN                                                   null

End of function create

Function authenticate:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 10
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3jOjl
function name:  authenticate
number of ops:  13
compiled vars:  !0 = $username, !1 = $password, !2 = $stored_pass
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  101     0  E >   RECV                                             !0      
          1        RECV                                             !1      
  103     2        FETCH_OBJ_R                                      ~3      'db'
          3        INIT_METHOD_CALL                                         ~3, 'getAccount'
          4        SEND_VAR_EX                                              !0
          5        DO_FCALL                                      0  $4      
          6        ASSIGN                                                   !2, $4
  105     7        TYPE_CHECK                                    2          !2
          8      > JMPZ                                                     ~6, ->10
  106     9    > > RETURN                                                   <false>
  109    10    >   IS_IDENTICAL                                     ~7      !1, !2
         11      > RETURN                                                   ~7
  110    12*     > RETURN                                                   null

End of function authenticate

End of class UserAccountManager.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
174.42 ms | 1412 KiB | 29 Q