3v4l.org

run code in 300+ PHP versions simultaneously
<?php class BankTransfer {} class CreditCard {} class Payment { private $creditCard; private $banTransfer; private function __construct(BankTransfer $bankTransfer = null, CreditCard $creditCard = null) { $this->bankTransfer = $bankTransfer; $this->creditCard = $creditCard; } public static function byCreditCard(CreditCard $creditCard) { $ccPayment = new self(); $ccPayment->creditCard = $creditCard; return $ccPayment; } public static function byBankTransfer(BankTransfer $bankTransfer) { return new self($bankTransfer, null); } public function isCreditCard() { return isset($this->creditCard); } public function isBankTransfer() { return isset($this->bankTransfer); } public function getBankTransfer() { if ($this->isBankTransfer()) { return $this->bankTransfer; } throw new \Exception(); } public function getCreditCard() { if ($this->isCreditCard()) { return $this->creditCard; } throw new \Exception(); } } class Booking { private $payment; public function __construct(Payment $payment) { $this->payment = $payment; } public function getPayment() { return $this->payment; } } //$payment = Payment::byBankTransfer(new BankTransfer()); $payment = Payment::byCreditCard(new CreditCard()); $booking = new Booking($payment); if ($booking->getPayment()->isCreditCard()) { echo 'credit card'; } if ($booking->getPayment()->isBankTransfer()) { echo 'bank transfer'; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 16
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 22
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
Branch analysis from position: 16
filename:       /in/iDq7G
function name:  (null)
number of ops:  23
compiled vars:  !0 = $payment, !1 = $booking
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   65     0  E >   INIT_STATIC_METHOD_CALL                                  'Payment', 'byCreditCard'
          1        NEW                                              $2      'CreditCard'
          2        DO_FCALL                                      0          
          3        SEND_VAR                                                 $2
          4        DO_FCALL                                      0  $4      
          5        ASSIGN                                                   !0, $4
   67     6        NEW                                              $6      'Booking'
          7        SEND_VAR_EX                                              !0
          8        DO_FCALL                                      0          
          9        ASSIGN                                                   !1, $6
   69    10        INIT_METHOD_CALL                                         !1, 'getPayment'
         11        DO_FCALL                                      0  $9      
         12        INIT_METHOD_CALL                                         $9, 'isCreditCard'
         13        DO_FCALL                                      0  $10     
         14      > JMPZ                                                     $10, ->16
   70    15    >   ECHO                                                     'credit+card'
   73    16    >   INIT_METHOD_CALL                                         !1, 'getPayment'
         17        DO_FCALL                                      0  $11     
         18        INIT_METHOD_CALL                                         $11, 'isBankTransfer'
         19        DO_FCALL                                      0  $12     
         20      > JMPZ                                                     $12, ->22
   74    21    >   ECHO                                                     'bank+transfer'
   75    22    > > RETURN                                                   1

Class BankTransfer: [no user functions]
Class CreditCard: [no user functions]
Class Payment:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iDq7G
function name:  __construct
number of ops:  7
compiled vars:  !0 = $bankTransfer, !1 = $creditCard
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   RECV_INIT                                        !0      null
          1        RECV_INIT                                        !1      null
   12     2        ASSIGN_OBJ                                               'bankTransfer'
          3        OP_DATA                                                  !0
   13     4        ASSIGN_OBJ                                               'creditCard'
          5        OP_DATA                                                  !1
   14     6      > RETURN                                                   null

End of function __construct

Function bycreditcard:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iDq7G
function name:  byCreditCard
number of ops:  8
compiled vars:  !0 = $creditCard, !1 = $ccPayment
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   RECV                                             !0      
   17     1        NEW                          self                $2      
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $2
   18     4        ASSIGN_OBJ                                               !1, 'creditCard'
          5        OP_DATA                                                  !0
   20     6      > RETURN                                                   !1
   21     7*     > RETURN                                                   null

End of function bycreditcard

Function bybanktransfer:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iDq7G
function name:  byBankTransfer
number of ops:  7
compiled vars:  !0 = $bankTransfer
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   RECV                                             !0      
   24     1        NEW                          self                $1      
          2        SEND_VAR_EX                                              !0
          3        SEND_VAL_EX                                              null
          4        DO_FCALL                                      0          
          5      > RETURN                                                   $1
   25     6*     > RETURN                                                   null

End of function bybanktransfer

Function iscreditcard:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iDq7G
function name:  isCreditCard
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   ISSET_ISEMPTY_PROP_OBJ                           ~0      'creditCard'
          1      > RETURN                                                   ~0
   29     2*     > RETURN                                                   null

End of function iscreditcard

Function isbanktransfer:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iDq7G
function name:  isBankTransfer
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   ISSET_ISEMPTY_PROP_OBJ                           ~0      'bankTransfer'
          1      > RETURN                                                   ~0
   33     2*     > RETURN                                                   null

End of function isbanktransfer

Function getbanktransfer:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 5
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/iDq7G
function name:  getBankTransfer
number of ops:  9
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   INIT_METHOD_CALL                                         'isBankTransfer'
          1        DO_FCALL                                      0  $0      
          2      > JMPZ                                                     $0, ->5
   37     3    >   FETCH_OBJ_R                                      ~1      'bankTransfer'
          4      > RETURN                                                   ~1
   40     5    >   NEW                                              $2      'Exception'
          6        DO_FCALL                                      0          
          7      > THROW                                         0          $2
   41     8*     > RETURN                                                   null

End of function getbanktransfer

Function getcreditcard:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 5
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/iDq7G
function name:  getCreditCard
number of ops:  9
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   INIT_METHOD_CALL                                         'isCreditCard'
          1        DO_FCALL                                      0  $0      
          2      > JMPZ                                                     $0, ->5
   45     3    >   FETCH_OBJ_R                                      ~1      'creditCard'
          4      > RETURN                                                   ~1
   48     5    >   NEW                                              $2      'Exception'
          6        DO_FCALL                                      0          
          7      > THROW                                         0          $2
   49     8*     > RETURN                                                   null

End of function getcreditcard

End of class Payment.

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

End of function __construct

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

End of function getpayment

End of class Booking.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
164.45 ms | 1403 KiB | 13 Q