3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace StatePatternPHP; interface DocumentManagement { public function review(); public function approve(); public function reject(); public function publish(); } abstract class State implements DocumentManagement { protected Document $document; public function __construct(Document $document) { $this->document = $document; } public function review() { throw new Exception("Document cannot be reviewed in this current state (". get_class($this).")"); } public function approve() { throw new Exception("Document cannot be approved in this current state (". get_class($this).")"); } public function publish() { throw new Exception("Document cannot be published in this current state (". get_class($this).")"); } public function reject() { throw new Exception("Document cannot be rejected in this current state (". get_class($this).")"); } } class Document implements DocumentManagement { private string $content; private State $currentState; private int $approvals = 0; public function __construct(string $content) { $this->content = $content; $this->currentState = new Draft($this); } public function getContent(): string { return $this->content; } public function setContent(string $content){ $this->content = $content; $this->currentState = new Draft($this); $this->approvals = 0; } public function setState(State $state){ $this->currentState = $state; } public function addApproval(){ $this->approvals++; } public function disapprove(){ $this->approvals--; } public function getApprovals(){ return $this->approvals; } public function review() { $this->currentState->review(); } public function approve() { $this->currentState->approve(); } public function publish() { $this->currentState->publish(); } public function reject() { $this->currentState->reject(); } } class Draft extends State { public function review() { $this->document->setState(new InReview($this->document)); } } class InReview extends State { public function approve() { $this->document->addApproval(); } public function publish() { if($this->document->getApprovals() > 2){ //needs 3 votes at least $this->document->setState(new Published($this->document)); }else{ parent::publish(); } } public function reject() { $this->document->disapprove(); } } class Published extends State { public function __construct(Document $document) { parent::__construct($document); print('document published !'); } } $document = new Document("hello world !"); $document->review(); $document->approve(); $document->approve(); $document->approve(); $document->publish();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RVVfZ
function name:  (null)
number of ops:  20
compiled vars:  !0 = $document
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   DECLARE_CLASS                                            'statepatternphp%5Cstate'
   33     1        DECLARE_CLASS                                            'statepatternphp%5Cdocument'
   78     2        DECLARE_CLASS                                            'statepatternphp%5Cdraft', 'statepatternphp%5Cstate'
   85     3        DECLARE_CLASS                                            'statepatternphp%5Cinreview', 'statepatternphp%5Cstate'
  103     4        DECLARE_CLASS                                            'statepatternphp%5Cpublished', 'statepatternphp%5Cstate'
  110     5        NEW                                              $1      'StatePatternPHP%5CDocument'
          6        SEND_VAL_EX                                              'hello+world+%21'
          7        DO_FCALL                                      0          
          8        ASSIGN                                                   !0, $1
  111     9        INIT_METHOD_CALL                                         !0, 'review'
         10        DO_FCALL                                      0          
  112    11        INIT_METHOD_CALL                                         !0, 'approve'
         12        DO_FCALL                                      0          
  113    13        INIT_METHOD_CALL                                         !0, 'approve'
         14        DO_FCALL                                      0          
  114    15        INIT_METHOD_CALL                                         !0, 'approve'
         16        DO_FCALL                                      0          
  115    17        INIT_METHOD_CALL                                         !0, 'publish'
         18        DO_FCALL                                      0          
         19      > RETURN                                                   1

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

End of function review

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

End of function approve

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

End of function reject

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

End of function publish

End of class StatePatternPHP\DocumentManagement.

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

End of function __construct

Function review:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/RVVfZ
function name:  review
number of ops:  11
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   NEW                                              $0      'StatePatternPHP%5CException'
          1        INIT_NS_FCALL_BY_NAME                                    'StatePatternPHP%5Cget_class'
          2        FETCH_THIS                                       $1      
          3        SEND_VAR_EX                                              $1
          4        DO_FCALL                                      0  $2      
          5        CONCAT                                           ~3      'Document+cannot+be+reviewed+in+this+current+state+%28', $2
          6        CONCAT                                           ~4      ~3, '%29'
          7        SEND_VAL_EX                                              ~4
          8        DO_FCALL                                      0          
          9      > THROW                                         0          $0
   19    10*     > RETURN                                                   null

End of function review

Function approve:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/RVVfZ
function name:  approve
number of ops:  11
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   NEW                                              $0      'StatePatternPHP%5CException'
          1        INIT_NS_FCALL_BY_NAME                                    'StatePatternPHP%5Cget_class'
          2        FETCH_THIS                                       $1      
          3        SEND_VAR_EX                                              $1
          4        DO_FCALL                                      0  $2      
          5        CONCAT                                           ~3      'Document+cannot+be+approved+in+this+current+state+%28', $2
          6        CONCAT                                           ~4      ~3, '%29'
          7        SEND_VAL_EX                                              ~4
          8        DO_FCALL                                      0          
          9      > THROW                                         0          $0
   22    10*     > RETURN                                                   null

End of function approve

Function publish:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/RVVfZ
function name:  publish
number of ops:  11
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   NEW                                              $0      'StatePatternPHP%5CException'
          1        INIT_NS_FCALL_BY_NAME                                    'StatePatternPHP%5Cget_class'
          2        FETCH_THIS                                       $1      
          3        SEND_VAR_EX                                              $1
          4        DO_FCALL                                      0  $2      
          5        CONCAT                                           ~3      'Document+cannot+be+published+in+this+current+state+%28', $2
          6        CONCAT                                           ~4      ~3, '%29'
          7        SEND_VAL_EX                                              ~4
          8        DO_FCALL                                      0          
          9      > THROW                                         0          $0
   26    10*     > RETURN                                                   null

End of function publish

Function reject:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/RVVfZ
function name:  reject
number of ops:  11
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   NEW                                              $0      'StatePatternPHP%5CException'
          1        INIT_NS_FCALL_BY_NAME                                    'StatePatternPHP%5Cget_class'
          2        FETCH_THIS                                       $1      
          3        SEND_VAR_EX                                              $1
          4        DO_FCALL                                      0  $2      
          5        CONCAT                                           ~3      'Document+cannot+be+rejected+in+this+current+state+%28', $2
          6        CONCAT                                           ~4      ~3, '%29'
          7        SEND_VAL_EX                                              ~4
          8        DO_FCALL                                      0          
          9      > THROW                                         0          $0
   30    10*     > RETURN                                                   null

End of function reject

End of class StatePatternPHP\State.

Class StatePatternPHP\Document:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RVVfZ
function name:  __construct
number of ops:  10
compiled vars:  !0 = $content
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E >   RECV                                             !0      
   40     1        ASSIGN_OBJ                                               'content'
          2        OP_DATA                                                  !0
   41     3        NEW                                              $3      'StatePatternPHP%5CDraft'
          4        FETCH_THIS                                       $4      
          5        SEND_VAR_EX                                              $4
          6        DO_FCALL                                      0          
          7        ASSIGN_OBJ                                               'currentState'
          8        OP_DATA                                                  $3
   42     9      > RETURN                                                   null

End of function __construct

Function getcontent:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RVVfZ
function name:  getContent
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   FETCH_OBJ_R                                      ~0      'content'
          1        VERIFY_RETURN_TYPE                                       ~0
          2      > RETURN                                                   ~0
   45     3*       VERIFY_RETURN_TYPE                                       
          4*     > RETURN                                                   null

End of function getcontent

Function setcontent:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RVVfZ
function name:  setContent
number of ops:  12
compiled vars:  !0 = $content
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   46     0  E >   RECV                                             !0      
   47     1        ASSIGN_OBJ                                               'content'
          2        OP_DATA                                                  !0
   48     3        NEW                                              $3      'StatePatternPHP%5CDraft'
          4        FETCH_THIS                                       $4      
          5        SEND_VAR_EX                                              $4
          6        DO_FCALL                                      0          
          7        ASSIGN_OBJ                                               'currentState'
          8        OP_DATA                                                  $3
   49     9        ASSIGN_OBJ                                               'approvals'
         10        OP_DATA                                                  0
   50    11      > RETURN                                                   null

End of function setcontent

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

End of function setstate

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

End of function addapproval

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

End of function disapprove

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

End of function getapprovals

Function review:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RVVfZ
function name:  review
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   64     0  E >   FETCH_OBJ_R                                      ~0      'currentState'
          1        INIT_METHOD_CALL                                         ~0, 'review'
          2        DO_FCALL                                      0          
   65     3      > RETURN                                                   null

End of function review

Function approve:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RVVfZ
function name:  approve
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   67     0  E >   FETCH_OBJ_R                                      ~0      'currentState'
          1        INIT_METHOD_CALL                                         ~0, 'approve'
          2        DO_FCALL                                      0          
   68     3      > RETURN                                                   null

End of function approve

Function publish:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RVVfZ
function name:  publish
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   70     0  E >   FETCH_OBJ_R                                      ~0      'currentState'
          1        INIT_METHOD_CALL                                         ~0, 'publish'
          2        DO_FCALL                                      0          
   71     3      > RETURN                                                   null

End of function publish

Function reject:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RVVfZ
function name:  reject
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   73     0  E >   FETCH_OBJ_R                                      ~0      'currentState'
          1        INIT_METHOD_CALL                                         ~0, 'reject'
          2        DO_FCALL                                      0          
   74     3      > RETURN                                                   null

End of function reject

End of class StatePatternPHP\Document.

Class StatePatternPHP\Draft:
Function review:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RVVfZ
function name:  review
number of ops:  10
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   81     0  E >   FETCH_OBJ_R                                      ~0      'document'
          1        INIT_METHOD_CALL                                         ~0, 'setState'
          2        NEW                                              $1      'StatePatternPHP%5CInReview'
          3        CHECK_FUNC_ARG                                           
          4        FETCH_OBJ_FUNC_ARG                               $2      'document'
          5        SEND_FUNC_ARG                                            $2
          6        DO_FCALL                                      0          
          7        SEND_VAR_NO_REF_EX                                       $1
          8        DO_FCALL                                      0          
   82     9      > RETURN                                                   null

End of function review

End of class StatePatternPHP\Draft.

Class StatePatternPHP\InReview:
Function approve:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RVVfZ
function name:  approve
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   88     0  E >   FETCH_OBJ_R                                      ~0      'document'
          1        INIT_METHOD_CALL                                         ~0, 'addApproval'
          2        DO_FCALL                                      0          
   89     3      > RETURN                                                   null

End of function approve

Function publish:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 15
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RVVfZ
function name:  publish
number of ops:  18
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   91     0  E >   FETCH_OBJ_R                                      ~0      'document'
          1        INIT_METHOD_CALL                                         ~0, 'getApprovals'
          2        DO_FCALL                                      0  $1      
          3        IS_SMALLER                                               2, $1
          4      > JMPZ                                                     ~2, ->15
   92     5    >   FETCH_OBJ_R                                      ~3      'document'
          6        INIT_METHOD_CALL                                         ~3, 'setState'
          7        NEW                                              $4      'StatePatternPHP%5CPublished'
          8        CHECK_FUNC_ARG                                           
          9        FETCH_OBJ_FUNC_ARG                               $5      'document'
         10        SEND_FUNC_ARG                                            $5
         11        DO_FCALL                                      0          
         12        SEND_VAR_NO_REF_EX                                       $4
         13        DO_FCALL                                      0          
         14      > JMP                                                      ->17
   94    15    >   INIT_STATIC_METHOD_CALL                                  'publish'
         16        DO_FCALL                                      0          
   96    17    > > RETURN                                                   null

End of function publish

Function reject:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RVVfZ
function name:  reject
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   98     0  E >   FETCH_OBJ_R                                      ~0      'document'
          1        INIT_METHOD_CALL                                         ~0, 'disapprove'
          2        DO_FCALL                                      0          
   99     3      > RETURN                                                   null

End of function reject

End of class StatePatternPHP\InReview.

Class StatePatternPHP\Published:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RVVfZ
function name:  __construct
number of ops:  6
compiled vars:  !0 = $document
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  104     0  E >   RECV                                             !0      
  105     1        INIT_STATIC_METHOD_CALL                                  
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
  106     4        ECHO                                                     'document+published+%21'
  107     5      > RETURN                                                   null

End of function __construct

End of class StatePatternPHP\Published.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
142.85 ms | 1424 KiB | 15 Q