3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Query { private $select; private $from; private $where = []; /** * Defines the column list (SELECT {$columns}) * * @param string $columns * @return this */ public function select($columns) { $this->select = $columns; return $this; } /** * Defines the table to select from (SELECT * FROM {$table}) * * @param string $table * @return this */ public function from($table) { $this->from = $table; return $this; } /** * List of conditions to AND (key = column, value = value) * e.g. $query->where(['name' => 'bob']) * * @param mixed[string] $conditions * @return $this */ public function where(array $conditions) { $this->where = $conditions; return $this; } /** * Adds an AND condition * * @param string $column * @param mixed $value * @return this */ public function where_and($column, $value) { $this->where[$column] = $value; return $this; } /** * Builds and executes the query, returning a PDOStatement * * @return PDOStatement */ public function execute() { $query = sprintf( 'SELECT %s FROM %s', $this->select, $this->from ); $placeholders = array(); if (!empty($this->where)) { $query .= ' WHERE '; $index = 0; foreach ($this->where as $column => $value) { if ($index > 0) { $query .= ' AND '; } $query .= sprintf('`%s`', $column); if ($value === null) { $query .= ' IS NULL'; } else { $placeholder = sprintf(':placeholder_%d', $index); $query .= ' = ' . $placeholder; $placeholders[$placeholder] = $value; } $index++; } } // $pdo = new PDO($dsn, $user, $pass, $opt); // $stmt = $pdo->prepare($query); // $stmt->execute($placeholders); // return $stmt; echo $query . "\n\n"; print_r($placeholders); } } class MyClass { public function getData($name, $club) { $query = new Query(); $query->select('name') ->from('users') ->where(['quote' => '1']); $this->getQueryPart($query); $result = $query->execute(); // return $result->fetchAll(PDO::FETCH_ASSOC); } protected function getQueryPart($query) { $query->where_and('email', 'email@foobar.com') ->where_and('status', 1); } } $my_class = new MyClass(); $data = $my_class->getData('name', 'club'); // print_r($data);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iKu2K
function name:  (null)
number of ops:  9
compiled vars:  !0 = $my_class, !1 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  121     0  E >   NEW                                              $2      'MyClass'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $2
  122     3        INIT_METHOD_CALL                                         !0, 'getData'
          4        SEND_VAL_EX                                              'name'
          5        SEND_VAL_EX                                              'club'
          6        DO_FCALL                                      0  $5      
          7        ASSIGN                                                   !1, $5
  123     8      > RETURN                                                   1

Class Query:
Function select:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iKu2K
function name:  select
number of ops:  6
compiled vars:  !0 = $columns
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
   15     1        ASSIGN_OBJ                                               'select'
          2        OP_DATA                                                  !0
   16     3        FETCH_THIS                                       ~2      
          4      > RETURN                                                   ~2
   17     5*     > RETURN                                                   null

End of function select

Function from:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iKu2K
function name:  from
number of ops:  6
compiled vars:  !0 = $table
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   RECV                                             !0      
   26     1        ASSIGN_OBJ                                               'from'
          2        OP_DATA                                                  !0
   27     3        FETCH_THIS                                       ~2      
          4      > RETURN                                                   ~2
   28     5*     > RETURN                                                   null

End of function from

Function where:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iKu2K
function name:  where
number of ops:  6
compiled vars:  !0 = $conditions
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   RECV                                             !0      
   38     1        ASSIGN_OBJ                                               'where'
          2        OP_DATA                                                  !0
   39     3        FETCH_THIS                                       ~2      
          4      > RETURN                                                   ~2
   40     5*     > RETURN                                                   null

End of function where

Function where_and:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iKu2K
function name:  where_and
number of ops:  8
compiled vars:  !0 = $column, !1 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   50     2        FETCH_OBJ_W                                      $2      'where'
          3        ASSIGN_DIM                                               $2, !0
          4        OP_DATA                                                  !1
   51     5        FETCH_THIS                                       ~4      
          6      > RETURN                                                   ~4
   52     7*     > RETURN                                                   null

End of function where_and

Function execute:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 42
Branch analysis from position: 12
2 jumps found. (Code = 77) Position 1 = 16, Position 2 = 41
Branch analysis from position: 16
2 jumps found. (Code = 78) Position 1 = 17, Position 2 = 41
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 21
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 30
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 39
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 21
Branch analysis from position: 41
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 41
Branch analysis from position: 42
filename:       /in/iKu2K
function name:  execute
number of ops:  48
compiled vars:  !0 = $query, !1 = $placeholders, !2 = $index, !3 = $value, !4 = $column, !5 = $placeholder
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   60     0  E >   INIT_FCALL                                               'sprintf'
   61     1        SEND_VAL                                                 'SELECT+%25s+FROM+%25s'
   62     2        FETCH_OBJ_R                                      ~6      'select'
          3        SEND_VAL                                                 ~6
   63     4        FETCH_OBJ_R                                      ~7      'from'
          5        SEND_VAL                                                 ~7
   60     6        DO_ICALL                                         $8      
          7        ASSIGN                                                   !0, $8
   66     8        ASSIGN                                                   !1, <array>
   68     9        ISSET_ISEMPTY_PROP_OBJ                           ~11     'where'
         10        BOOL_NOT                                         ~12     ~11
         11      > JMPZ                                                     ~12, ->42
   69    12    >   ASSIGN_OP                                     8          !0, '+WHERE+'
   71    13        ASSIGN                                                   !2, 0
   72    14        FETCH_OBJ_R                                      ~15     'where'
         15      > FE_RESET_R                                       $16     ~15, ->41
         16    > > FE_FETCH_R                                       ~17     $16, !3, ->41
         17    >   ASSIGN                                                   !4, ~17
   73    18        IS_SMALLER                                               0, !2
         19      > JMPZ                                                     ~19, ->21
   74    20    >   ASSIGN_OP                                     8          !0, '+AND+'
   77    21    >   INIT_FCALL                                               'sprintf'
         22        SEND_VAL                                                 '%60%25s%60'
         23        SEND_VAR                                                 !4
         24        DO_ICALL                                         $21     
         25        ASSIGN_OP                                     8          !0, $21
   79    26        TYPE_CHECK                                    2          !3
         27      > JMPZ                                                     ~23, ->30
   80    28    >   ASSIGN_OP                                     8          !0, '+IS+NULL'
   79    29      > JMP                                                      ->39
   82    30    >   INIT_FCALL                                               'sprintf'
         31        SEND_VAL                                                 '%3Aplaceholder_%25d'
         32        SEND_VAR                                                 !2
         33        DO_ICALL                                         $25     
         34        ASSIGN                                                   !5, $25
   84    35        CONCAT                                           ~27     '+%3D+', !5
         36        ASSIGN_OP                                     8          !0, ~27
   85    37        ASSIGN_DIM                                               !1, !5
         38        OP_DATA                                                  !3
   88    39    >   PRE_INC                                                  !2
   72    40      > JMP                                                      ->16
         41    >   FE_FREE                                                  $16
   97    42    >   CONCAT                                           ~31     !0, '%0A%0A'
         43        ECHO                                                     ~31
   98    44        INIT_FCALL                                               'print_r'
         45        SEND_VAR                                                 !1
         46        DO_ICALL                                                 
   99    47      > RETURN                                                   null

End of function execute

End of class Query.

Class MyClass:
Function getdata:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iKu2K
function name:  getData
number of ops:  21
compiled vars:  !0 = $name, !1 = $club, !2 = $query, !3 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  103     0  E >   RECV                                             !0      
          1        RECV                                             !1      
  104     2        NEW                                              $4      'Query'
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !2, $4
  105     5        INIT_METHOD_CALL                                         !2, 'select'
          6        SEND_VAL_EX                                              'name'
          7        DO_FCALL                                      0  $7      
  106     8        INIT_METHOD_CALL                                         $7, 'from'
          9        SEND_VAL_EX                                              'users'
         10        DO_FCALL                                      0  $8      
  107    11        INIT_METHOD_CALL                                         $8, 'where'
         12        SEND_VAL_EX                                              <array>
         13        DO_FCALL                                      0          
  109    14        INIT_METHOD_CALL                                         'getQueryPart'
         15        SEND_VAR_EX                                              !2
         16        DO_FCALL                                      0          
  110    17        INIT_METHOD_CALL                                         !2, 'execute'
         18        DO_FCALL                                      0  $11     
         19        ASSIGN                                                   !3, $11
  113    20      > RETURN                                                   null

End of function getdata

Function getquerypart:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iKu2K
function name:  getQueryPart
number of ops:  10
compiled vars:  !0 = $query
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  115     0  E >   RECV                                             !0      
  116     1        INIT_METHOD_CALL                                         !0, 'where_and'
          2        SEND_VAL_EX                                              'email'
          3        SEND_VAL_EX                                              'email%40foobar.com'
          4        DO_FCALL                                      0  $1      
  117     5        INIT_METHOD_CALL                                         $1, 'where_and'
          6        SEND_VAL_EX                                              'status'
          7        SEND_VAL_EX                                              1
          8        DO_FCALL                                      0          
  118     9      > RETURN                                                   null

End of function getquerypart

End of class MyClass.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
151.27 ms | 1016 KiB | 15 Q