3v4l.org

run code in 500+ PHP versions simultaneously
<?php interface DbAdapterInterface { /** * @param DateTime $date * @return string */ public function convertFromDateTime(DateTime $date); /** * @param DateTime $date * @return array */ public function convertToDateTime(array $row); } class MySqlAdapter implements DbAdapterInterface { public function convertFromDateTime(DateTime $date) { return $date->format('Y-m-d H:i:s'); } public function convertToDateTime(array $row) { return new DateTime($row['date']); } } class OracleAdapter implements DbAdapterInterface { public function convertFromDateTime(DateTime $date) { return $date->getTimestamp(); } public function convertToDateTime(array $row) { return new DateTime('@'.$row['date']); } } class Db { /** @var DbAdapterInterface $adapter */ private $adapter; public function setAdapter(DbAdapterInterface $adapter) { $this->adapter = $adapter; } public function insert($data) { $date = $data['date']; $dateString = $this->adapter->convertFromDateTime($date); // do your insert return $dateString; } public function findById($id) { // fetch row by id here, this is just an example, I'm using the id as the date string $row = ['date' => $id]; $row = $this->adapter->convertToDateTime($row); // do your insert return $row; } } // Example $data = [ 'date' => new DateTime(), ]; $db = new Db(); $db->setAdapter(new MySqlAdapter()); echo $db->insert($data)."\n"; $db->setAdapter(new OracleAdapter()); echo $db->insert($data)."\n"; $time = '1493308146'; var_dump( $db->findById($time)); $db->setAdapter(new MySqlAdapter()); $time = '2014-09-18 22:00:00'; var_dump( $db->findById($time));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/pYgbE
function name:  (null)
number of ops:  49
compiled vars:  !0 = $data, !1 = $db, !2 = $time
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   18     0  E >   DECLARE_CLASS                                                'mysqladapter'
   31     1        DECLARE_CLASS                                                'oracleadapter'
   76     2        NEW                                                  $3      'DateTime'
          3        DO_FCALL                                          0          
          4        INIT_ARRAY                                           ~5      $3, 'date'
   75     5        ASSIGN                                                       !0, ~5
   79     6        NEW                                                  $7      'Db'
          7        DO_FCALL                                          0          
          8        ASSIGN                                                       !1, $7
   80     9        INIT_METHOD_CALL                                             !1, 'setAdapter'
         10        NEW                                                  $10     'MySqlAdapter'
         11        DO_FCALL                                          0          
         12        SEND_VAR_NO_REF_EX                                           $10
         13        DO_FCALL                                          0          
   81    14        INIT_METHOD_CALL                                             !1, 'insert'
         15        SEND_VAR_EX                                                  !0
         16        DO_FCALL                                          0  $13     
         17        CONCAT                                               ~14     $13, '%0A'
         18        ECHO                                                         ~14
   82    19        INIT_METHOD_CALL                                             !1, 'setAdapter'
         20        NEW                                                  $15     'OracleAdapter'
         21        DO_FCALL                                          0          
         22        SEND_VAR_NO_REF_EX                                           $15
         23        DO_FCALL                                          0          
   83    24        INIT_METHOD_CALL                                             !1, 'insert'
         25        SEND_VAR_EX                                                  !0
         26        DO_FCALL                                          0  $18     
         27        CONCAT                                               ~19     $18, '%0A'
         28        ECHO                                                         ~19
   85    29        ASSIGN                                                       !2, '1493308146'
   86    30        INIT_FCALL                                                   'var_dump'
         31        INIT_METHOD_CALL                                             !1, 'findById'
         32        SEND_VAR_EX                                                  !2
         33        DO_FCALL                                          0  $21     
         34        SEND_VAR                                                     $21
         35        DO_ICALL                                                     
   88    36        INIT_METHOD_CALL                                             !1, 'setAdapter'
         37        NEW                                                  $23     'MySqlAdapter'
         38        DO_FCALL                                          0          
         39        SEND_VAR_NO_REF_EX                                           $23
         40        DO_FCALL                                          0          
   89    41        ASSIGN                                                       !2, '2014-09-18+22%3A00%3A00'
   90    42        INIT_FCALL                                                   'var_dump'
         43        INIT_METHOD_CALL                                             !1, 'findById'
         44        SEND_VAR_EX                                                  !2
         45        DO_FCALL                                          0  $27     
         46        SEND_VAR                                                     $27
         47        DO_ICALL                                                     
         48      > RETURN                                                       1

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

End of function convertfromdatetime

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

End of function converttodatetime

End of class DbAdapterInterface.

Class MySqlAdapter:
Function convertfromdatetime:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/pYgbE
function name:  convertFromDateTime
number of ops:  6
compiled vars:  !0 = $date
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   20     0  E >   RECV                                                 !0      
   22     1        INIT_METHOD_CALL                                             !0, 'format'
          2        SEND_VAL_EX                                                  'Y-m-d+H%3Ai%3As'
          3        DO_FCALL                                          0  $1      
          4      > RETURN                                                       $1
   23     5*     > RETURN                                                       null

End of function convertfromdatetime

Function converttodatetime:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/pYgbE
function name:  convertToDateTime
number of ops:  8
compiled vars:  !0 = $row
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   25     0  E >   RECV                                                 !0      
   27     1        NEW                                                  $1      'DateTime'
          2        CHECK_FUNC_ARG                                               
          3        FETCH_DIM_FUNC_ARG                                   $2      !0, 'date'
          4        SEND_FUNC_ARG                                                $2
          5        DO_FCALL                                          0          
          6      > RETURN                                                       $1
   28     7*     > RETURN                                                       null

End of function converttodatetime

End of class MySqlAdapter.

Class OracleAdapter:
Function convertfromdatetime:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/pYgbE
function name:  convertFromDateTime
number of ops:  5
compiled vars:  !0 = $date
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   33     0  E >   RECV                                                 !0      
   35     1        INIT_METHOD_CALL                                             !0, 'getTimestamp'
          2        DO_FCALL                                          0  $1      
          3      > RETURN                                                       $1
   36     4*     > RETURN                                                       null

End of function convertfromdatetime

Function converttodatetime:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/pYgbE
function name:  convertToDateTime
number of ops:  8
compiled vars:  !0 = $row
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   38     0  E >   RECV                                                 !0      
   40     1        NEW                                                  $1      'DateTime'
          2        FETCH_DIM_R                                          ~2      !0, 'date'
          3        CONCAT                                               ~3      '%40', ~2
          4        SEND_VAL_EX                                                  ~3
          5        DO_FCALL                                          0          
          6      > RETURN                                                       $1
   41     7*     > RETURN                                                       null

End of function converttodatetime

End of class OracleAdapter.

Class Db:
Function setadapter:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/pYgbE
function name:  setAdapter
number of ops:  4
compiled vars:  !0 = $adapter
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   49     0  E >   RECV                                                 !0      
   51     1        ASSIGN_OBJ                                                   'adapter'
          2        OP_DATA                                                      !0
   52     3      > RETURN                                                       null

End of function setadapter

Function insert:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/pYgbE
function name:  insert
number of ops:  10
compiled vars:  !0 = $data, !1 = $date, !2 = $dateString
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   54     0  E >   RECV                                                 !0      
   56     1        FETCH_DIM_R                                          ~3      !0, 'date'
          2        ASSIGN                                                       !1, ~3
   57     3        FETCH_OBJ_R                                          ~5      'adapter'
          4        INIT_METHOD_CALL                                             ~5, 'convertFromDateTime'
          5        SEND_VAR_EX                                                  !1
          6        DO_FCALL                                          0  $6      
          7        ASSIGN                                                       !2, $6
   59     8      > RETURN                                                       !2
   60     9*     > RETURN                                                       null

End of function insert

Function findbyid:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/pYgbE
function name:  findById
number of ops:  10
compiled vars:  !0 = $id, !1 = $row
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   62     0  E >   RECV                                                 !0      
   65     1        INIT_ARRAY                                           ~2      !0, 'date'
          2        ASSIGN                                                       !1, ~2
   66     3        FETCH_OBJ_R                                          ~4      'adapter'
          4        INIT_METHOD_CALL                                             ~4, 'convertToDateTime'
          5        SEND_VAR_EX                                                  !1
          6        DO_FCALL                                          0  $5      
          7        ASSIGN                                                       !1, $5
   68     8      > RETURN                                                       !1
   69     9*     > RETURN                                                       null

End of function findbyid

End of class Db.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
277.07 ms | 2201 KiB | 15 Q