3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Decimal { private string $value; public function __construct(string $value) { $this->value = $value; } public function toFixed(int $scale): string { // rough example for adjusting precision - do not use return bcadd($this->value, '0', $scale); } } class Entity { #[ORM\Column(type: 'decimal', precision: 10, scale: 4)] private string $subtotal = '0.0'; // name as desired public function getSubtotalDecimal(): Decimal { return new Decimal($this->subtotal, 4); } public function getSubtotal(): string { return $this->subtotal; } // see Union Types public function setSubtotal(string|Decimal $subtotal): self { if ($subtotal instanceof Decimal) { $subtotal = $subtotal->toFixed(4); } $this->subtotal = $subtotal; return $this; } } $decimal = new Decimal("1337.987654321"); $entity = new Entity(); $entity->setSubtotal($decimal); echo $entity->getSubtotal() . \PHP_EOL; echo $entity->getSubtotalDecimal()->toFixed(4) . \PHP_EOL;
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/D4LDE
function name:  (null)
number of ops:  22
compiled vars:  !0 = $decimal, !1 = $entity
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   NEW                                              $2      'Decimal'
          1        SEND_VAL_EX                                              '1337.987654321'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $2
   49     4        NEW                                              $5      'Entity'
          5        DO_FCALL                                      0          
          6        ASSIGN                                                   !1, $5
   50     7        INIT_METHOD_CALL                                         !1, 'setSubtotal'
          8        SEND_VAR_EX                                              !0
          9        DO_FCALL                                      0          
   51    10        INIT_METHOD_CALL                                         !1, 'getSubtotal'
         11        DO_FCALL                                      0  $9      
         12        CONCAT                                           ~10     $9, '%0A'
         13        ECHO                                                     ~10
   53    14        INIT_METHOD_CALL                                         !1, 'getSubtotalDecimal'
         15        DO_FCALL                                      0  $11     
         16        INIT_METHOD_CALL                                         $11, 'toFixed'
         17        SEND_VAL_EX                                              4
         18        DO_FCALL                                      0  $12     
         19        CONCAT                                           ~13     $12, '%0A'
         20        ECHO                                                     ~13
   54    21      > RETURN                                                   1

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

End of function __construct

Function tofixed:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/D4LDE
function name:  toFixed
number of ops:  12
compiled vars:  !0 = $scale
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
   15     1        INIT_FCALL_BY_NAME                                       'bcadd'
          2        CHECK_FUNC_ARG                                           
          3        FETCH_OBJ_FUNC_ARG                               $1      'value'
          4        SEND_FUNC_ARG                                            $1
          5        SEND_VAL_EX                                              '0'
          6        SEND_VAR_EX                                              !0
          7        DO_FCALL                                      0  $2      
          8        VERIFY_RETURN_TYPE                                       $2
          9      > RETURN                                                   $2
   16    10*       VERIFY_RETURN_TYPE                                       
         11*     > RETURN                                                   null

End of function tofixed

End of class Decimal.

Class Entity:
Function getsubtotaldecimal:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/D4LDE
function name:  getSubtotalDecimal
number of ops:  10
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   NEW                                              $0      'Decimal'
          1        CHECK_FUNC_ARG                                           
          2        FETCH_OBJ_FUNC_ARG                               $1      'subtotal'
          3        SEND_FUNC_ARG                                            $1
          4        SEND_VAL_EX                                              4
          5        DO_FCALL                                      0          
          6        VERIFY_RETURN_TYPE                                       $0
          7      > RETURN                                                   $0
   28     8*       VERIFY_RETURN_TYPE                                       
          9*     > RETURN                                                   null

End of function getsubtotaldecimal

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

End of function getsubtotal

Function setsubtotal:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 7
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
filename:       /in/D4LDE
function name:  setSubtotal
number of ops:  14
compiled vars:  !0 = $subtotal
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   RECV                                             !0      
   38     1        INSTANCEOF                                               !0, 'Decimal'
          2      > JMPZ                                                     ~1, ->7
   39     3    >   INIT_METHOD_CALL                                         !0, 'toFixed'
          4        SEND_VAL_EX                                              4
          5        DO_FCALL                                      0  $2      
          6        ASSIGN                                                   !0, $2
   41     7    >   ASSIGN_OBJ                                               'subtotal'
          8        OP_DATA                                                  !0
   43     9        FETCH_THIS                                       ~5      
         10        VERIFY_RETURN_TYPE                                       ~5
         11      > RETURN                                                   ~5
   44    12*       VERIFY_RETURN_TYPE                                       
         13*     > RETURN                                                   null

End of function setsubtotal

End of class Entity.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
135.6 ms | 1000 KiB | 13 Q