3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace Exercise_One; /** * Zillow exercise one * * Take an associative array and sort it based on columns and directions specified. (PSR1 Standard) * * @author Joe Rocha <joe.rocha@me.com> */ class Singleton { /** * @var Singleton The reference to *Singleton* instance of this class */ protected static $instance; /** * Returns sorted multidimensional, associative array * * @param array $array Data to sort. * @param string $sortKey Key to sort by. * @param string $dir Direction to sort ('ASC' || 'DESC'). * @param bool $nat Natural sorting. * * @return bool $instance True if no other instance. */ public static function sortBy( array &$array, $sortKey = null, $dir = 'ASC', $nat = false ) { if ( null === static::$instance ) { static::$instance = new static(); }; function sortLogic( $key, $dir, $nat ) { return function( $a, $b ) use ( $key, $dir, $nat ) { if ( true === $nat ){ return strnatcmp( 'ASC' === $dir ? $a : $b [ $key ], 'ASC' === $dir ? $b : $a [ $key ] ); } else { return $dir === 'ASC' ? $a[$key] > $b[$key] : $a[$key] < $b[$key]; } }; } uasort( $array, sortLogic( $sortKey, $dir, $nat ) ); return static::$instance; } /** * Make constructor private, so no one can call "new Class". */ private function __construct() {} /** * Make clone magic method private, so no one can clone instance. */ private function __clone() {} /** * Make sleep magic method private, so no one can serialize instance. */ private function __sleep() {} /** * Make wakeup magic method private, so no one can unserialize instance. */ private function __wakeup() {} } // Dummy Data for testing $data = array(); $data[] = array('id' => 1, 'number' => '1', 'street' => 'Battery St', 'unit' => '1', 'rent' => 1200); $data[] = array('id' => 10, 'number' => '10', 'street' => 'Battery St', 'unit' => '3', 'rent' => 1800); $data[] = array('id' => 2, 'number' => '2', 'street' => 'Leavenworth St', 'unit' => '11', 'rent' => 800); $data[] = array('id' => 12, 'number' => '12', 'street' => 'Battery St', 'unit' => '10', 'rent' => 3400); $data[] = array('id' => 1, 'number' => '14', 'street' => 'Leavenworth St', 'unit' => '10', 'rent' => 1450); $data[] = array('id' => 1, 'number' => '01', 'street' => 'Battery St', 'unit' => '5', 'rent' => 1000); // Sanity check Singleton::sortBy( $data, 'number', 'DESC', true ); print_r( $data );
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/TP2OT
function name:  (null)
number of ops:  23
compiled vars:  !0 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   70     0  E >   ASSIGN                                                   !0, <array>
   71     1        ASSIGN_DIM                                               !0
          2        OP_DATA                                                  <array>
   72     3        ASSIGN_DIM                                               !0
          4        OP_DATA                                                  <array>
   73     5        ASSIGN_DIM                                               !0
          6        OP_DATA                                                  <array>
   74     7        ASSIGN_DIM                                               !0
          8        OP_DATA                                                  <array>
   75     9        ASSIGN_DIM                                               !0
         10        OP_DATA                                                  <array>
   76    11        ASSIGN_DIM                                               !0
         12        OP_DATA                                                  <array>
   78    13        INIT_STATIC_METHOD_CALL                                  'Exercise_One%5CSingleton', 'sortBy'
         14        SEND_REF                                                 !0
         15        SEND_VAL                                                 'number'
         16        SEND_VAL                                                 'DESC'
         17        SEND_VAL                                                 <true>
         18        DO_FCALL                                      0          
   79    19        INIT_NS_FCALL_BY_NAME                                    'Exercise_One%5Cprint_r'
         20        SEND_VAR_EX                                              !0
         21        DO_FCALL                                      0          
         22      > RETURN                                                   1

Function %00exercise_one%5Csortlogic%2Fin%2FTP2OT%3A33%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/TP2OT
function name:  Exercise_One\sortLogic
number of ops:  9
compiled vars:  !0 = $key, !1 = $dir, !2 = $nat
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   34     3        DECLARE_LAMBDA_FUNCTION                                  '%00exercise_one%5C%7Bclosure%7D%2Fin%2FTP2OT%3A34%241'
          4        BIND_LEXICAL                                             ~3, !0
          5        BIND_LEXICAL                                             ~3, !1
          6        BIND_LEXICAL                                             ~3, !2
   40     7      > RETURN                                                   ~3
   41     8*     > RETURN                                                   null

End of function %00exercise_one%5Csortlogic%2Fin%2FTP2OT%3A33%240

Function %00exercise_one%5C%7Bclosure%7D%2Fin%2FTP2OT%3A34%241:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 25
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 12
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 19
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 19
Branch analysis from position: 17
Branch analysis from position: 19
Branch analysis from position: 25
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 32
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 36
Branch analysis from position: 36
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 32
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/TP2OT
function name:  Exercise_One\{closure}
number of ops:  38
compiled vars:  !0 = $a, !1 = $b, !2 = $key, !3 = $dir, !4 = $nat
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        BIND_STATIC                                              !2
          3        BIND_STATIC                                              !3
          4        BIND_STATIC                                              !4
   35     5        TYPE_CHECK                                    8          !4
          6      > JMPZ                                                     ~5, ->25
   36     7    >   INIT_NS_FCALL_BY_NAME                                    'Exercise_One%5Cstrnatcmp'
          8        IS_IDENTICAL                                             !3, 'ASC'
          9      > JMPZ                                                     ~6, ->12
         10    >   QM_ASSIGN                                        ~7      !0
         11      > JMP                                                      ->14
         12    >   FETCH_DIM_R                                      ~8      !1, !2
         13        QM_ASSIGN                                        ~7      ~8
         14    >   SEND_VAL_EX                                              ~7
         15        IS_IDENTICAL                                             !3, 'ASC'
         16      > JMPZ                                                     ~9, ->19
         17    >   QM_ASSIGN                                        ~10     !1
         18      > JMP                                                      ->21
         19    >   FETCH_DIM_R                                      ~11     !0, !2
         20        QM_ASSIGN                                        ~10     ~11
         21    >   SEND_VAL_EX                                              ~10
         22        DO_FCALL                                      0  $12     
         23      > RETURN                                                   $12
         24*       JMP                                                      ->37
   38    25    >   IS_IDENTICAL                                             !3, 'ASC'
         26      > JMPZ                                                     ~13, ->32
         27    >   FETCH_DIM_R                                      ~14     !0, !2
         28        FETCH_DIM_R                                      ~15     !1, !2
         29        IS_SMALLER                                       ~16     ~15, ~14
         30        QM_ASSIGN                                        ~17     ~16
         31      > JMP                                                      ->36
         32    >   FETCH_DIM_R                                      ~18     !0, !2
         33        FETCH_DIM_R                                      ~19     !1, !2
         34        IS_SMALLER                                       ~20     ~18, ~19
         35        QM_ASSIGN                                        ~17     ~20
         36    > > RETURN                                                   ~17
   40    37*     > RETURN                                                   null

End of function %00exercise_one%5C%7Bclosure%7D%2Fin%2FTP2OT%3A34%241

Class Exercise_One\Singleton:
Function sortby:
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 = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/TP2OT
function name:  sortBy
number of ops:  24
compiled vars:  !0 = $array, !1 = $sortKey, !2 = $dir, !3 = $nat
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      null
          2        RECV_INIT                                        !2      'ASC'
          3        RECV_INIT                                        !3      <false>
   29     4        FETCH_STATIC_PROP_R          unknown             ~4      'instance'
          5        TYPE_CHECK                                    2          ~4
          6      > JMPZ                                                     ~5, ->11
   30     7    >   NEW                          static              $7      
          8        DO_FCALL                                      0          
          9        ASSIGN_STATIC_PROP                                       'instance'
         10        OP_DATA                                                  $7
   33    11    >   DECLARE_FUNCTION                                         'exercise_one%5Csortlogic'
   42    12        INIT_NS_FCALL_BY_NAME                                    'Exercise_One%5Cuasort'
         13        SEND_VAR_EX                                              !0
         14        INIT_NS_FCALL_BY_NAME                                    'Exercise_One%5CsortLogic'
         15        SEND_VAR_EX                                              !1
         16        SEND_VAR_EX                                              !2
         17        SEND_VAR_EX                                              !3
         18        DO_FCALL                                      0  $9      
         19        SEND_VAR_NO_REF_EX                                       $9
         20        DO_FCALL                                      0          
   44    21        FETCH_STATIC_PROP_R          unknown             ~11     'instance'
         22      > RETURN                                                   ~11
   45    23*     > RETURN                                                   null

End of function sortby

Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/TP2OT
function name:  __construct
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E > > RETURN                                                   null

End of function __construct

Function __clone:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/TP2OT
function name:  __clone
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   55     0  E > > RETURN                                                   null

End of function __clone

Function __sleep:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/TP2OT
function name:  __sleep
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   60     0  E > > RETURN                                                   null

End of function __sleep

Function __wakeup:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/TP2OT
function name:  __wakeup
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   65     0  E > > RETURN                                                   null

End of function __wakeup

End of class Exercise_One\Singleton.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
160.27 ms | 1411 KiB | 20 Q