3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Image { protected static string $class; protected string $image_path; protected static string $sizes; protected static array $image_widths = array(); protected string $alt; public function __construct(string $image_path, string $alt = '') { $this->image_path = $image_path; $this->alt = $alt; } public function generateHTML(): string { // Generate array of path and width strings for srcset foreach (self::$image_widths as $image_width) { $generated_path = $this->generatePath($this->image_path, $image_width); $srcset[] = "{$generated_path} {$image_width}w"; } return sprintf( '<div class="%s">' . '<img sizes="%s" srcset="%s" src="%s" alt="%s" />' . '</div>', self::$class, self::$sizes, implode(',', $srcset), end($srcset), $this->alt ); } // Replaces [-IMAGE_SIZE-] in the html with array width value protected function generatePath(string $image_path, $arr_pos): string { return str_replace("[-IMAGE_SIZE-]", $arr_pos, $image_path); } } class Image_P_Square extends Image { public function __construct(string $image_path, string $alt = '') { parent::__construct($image_path, $alt); static::$class = "ratio-box r1-1"; static::$sizes = "50vw"; static::$image_widths = array(192, 384, 576, 768); } } class Project { protected string $link; protected string $title; protected string $description; /** @var Image[] */ protected array $images; public function __construct( string $link, string $title, string $description, array $images = [] ) { $this->link = $link; $this->title = $title; $this->description = $description; $this->images = []; foreach ($images as $image) { $this->addImage($image); } } public function generateHTML(): string { $imageHTML = ''; foreach ($this->images as $image) { $imageHTML .= $image->generateHTML(); } return sprintf( '<article class="project-single-container" data-project="%s">' . '<div class="project-single">' . '<div class="project-header">' . '<a class="project-details">Details</a>' . '<h2 class="project-title">%s</h2>' . '<a class="project-close">Close</a>' . '</div>' . '<div class="project-content">' . '<div class="project-details-inner">%s</div>' . '<div class="images-wrapper">%s</div>' . '</div>' . '</div>' . '</article>', $this->link, $this->title, $this->description, $imageHTML ); } public function addImage(Image $image): void { $this->images[] = $image; } } // Initialise here. $image1 = new Image_P_Square( "https://res.cloudinary.com/demo/image/upload/c_scale,w_[-IMAGE_SIZE-]/lady.jpg", "this is alt" ); $project = new Project( "test_link", "test_title", "<p>Test description</p>" ); $project->addImage($image1); echo $project->generateHTML();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/NhAke
function name:  (null)
number of ops:  18
compiled vars:  !0 = $image1, !1 = $project
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  120     0  E >   NEW                                              $2      'Image_P_Square'
  121     1        SEND_VAL_EX                                              'https%3A%2F%2Fres.cloudinary.com%2Fdemo%2Fimage%2Fupload%2Fc_scale%2Cw_%5B-IMAGE_SIZE-%5D%2Flady.jpg'
  122     2        SEND_VAL_EX                                              'this+is+alt'
  120     3        DO_FCALL                                      0          
          4        ASSIGN                                                   !0, $2
  125     5        NEW                                              $5      'Project'
  126     6        SEND_VAL_EX                                              'test_link'
  127     7        SEND_VAL_EX                                              'test_title'
  128     8        SEND_VAL_EX                                              '%3Cp%3ETest+description%3C%2Fp%3E'
  125     9        DO_FCALL                                      0          
         10        ASSIGN                                                   !1, $5
  131    11        INIT_METHOD_CALL                                         !1, 'addImage'
         12        SEND_VAR_EX                                              !0
         13        DO_FCALL                                      0          
  133    14        INIT_METHOD_CALL                                         !1, 'generateHTML'
         15        DO_FCALL                                      0  $9      
         16        ECHO                                                     $9
         17      > RETURN                                                   1

Class Image:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/NhAke
function name:  __construct
number of ops:  7
compiled vars:  !0 = $image_path, !1 = $alt
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      ''
   12     2        ASSIGN_OBJ                                               'image_path'
          3        OP_DATA                                                  !0
   13     4        ASSIGN_OBJ                                               'alt'
          5        OP_DATA                                                  !1
   14     6      > RETURN                                                   null

End of function __construct

Function generatehtml:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 17
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 17
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
filename:       /in/NhAke
function name:  generateHTML
number of ops:  40
compiled vars:  !0 = $image_width, !1 = $generated_path, !2 = $srcset
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   FETCH_STATIC_PROP_R          unknown             ~3      'image_widths'
          1      > FE_RESET_R                                       $4      ~3, ->17
          2    > > FE_FETCH_R                                               $4, !0, ->17
   20     3    >   INIT_METHOD_CALL                                         'generatePath'
          4        CHECK_FUNC_ARG                                           
          5        FETCH_OBJ_FUNC_ARG                               $5      'image_path'
          6        SEND_FUNC_ARG                                            $5
          7        SEND_VAR_EX                                              !0
          8        DO_FCALL                                      0  $6      
          9        ASSIGN                                                   !1, $6
   22    10        ROPE_INIT                                     4  ~10     !1
         11        ROPE_ADD                                      1  ~10     ~10, '+'
         12        ROPE_ADD                                      2  ~10     ~10, !0
         13        ROPE_END                                      3  ~9      ~10, 'w'
         14        ASSIGN_DIM                                               !2
         15        OP_DATA                                                  ~9
   19    16      > JMP                                                      ->2
         17    >   FE_FREE                                                  $4
   25    18        INIT_FCALL                                               'sprintf'
   28    19        SEND_VAL                                                 '%3Cdiv+class%3D%22%25s%22%3E%3Cimg+sizes%3D%22%25s%22+srcset%3D%22%25s%22+src%3D%22%25s%22+alt%3D%22%25s%22+%2F%3E%3C%2Fdiv%3E'
   29    20        FETCH_STATIC_PROP_R          unknown             ~12     'class'
         21        SEND_VAL                                                 ~12
   30    22        FETCH_STATIC_PROP_R          unknown             ~13     'sizes'
         23        SEND_VAL                                                 ~13
   31    24        INIT_FCALL                                               'implode'
         25        SEND_VAL                                                 '%2C'
         26        SEND_VAR                                                 !2
         27        DO_ICALL                                         $14     
         28        SEND_VAR                                                 $14
   32    29        INIT_FCALL                                               'end'
         30        SEND_REF                                                 !2
         31        DO_ICALL                                         $15     
         32        SEND_VAR                                                 $15
   33    33        FETCH_OBJ_R                                      ~16     'alt'
         34        SEND_VAL                                                 ~16
   25    35        DO_ICALL                                         $17     
   33    36        VERIFY_RETURN_TYPE                                       $17
         37      > RETURN                                                   $17
   35    38*       VERIFY_RETURN_TYPE                                       
         39*     > RETURN                                                   null

End of function generatehtml

Function generatepath:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/NhAke
function name:  generatePath
number of ops:  11
compiled vars:  !0 = $image_path, !1 = $arr_pos
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   40     2        INIT_FCALL                                               'str_replace'
          3        SEND_VAL                                                 '%5B-IMAGE_SIZE-%5D'
          4        SEND_VAR                                                 !1
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $2      
          7        VERIFY_RETURN_TYPE                                       $2
          8      > RETURN                                                   $2
   41     9*       VERIFY_RETURN_TYPE                                       
         10*     > RETURN                                                   null

End of function generatepath

End of class Image.

Class Image_P_Square:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/NhAke
function name:  __construct
number of ops:  13
compiled vars:  !0 = $image_path, !1 = $alt
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   46     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      ''
   48     2        INIT_STATIC_METHOD_CALL                                  
          3        SEND_VAR_EX                                              !0
          4        SEND_VAR_EX                                              !1
          5        DO_FCALL                                      0          
   50     6        ASSIGN_STATIC_PROP                                       'class'
          7        OP_DATA                                                  'ratio-box+r1-1'
   51     8        ASSIGN_STATIC_PROP                                       'sizes'
          9        OP_DATA                                                  '50vw'
   52    10        ASSIGN_STATIC_PROP                                       'image_widths'
         11        OP_DATA                                                  <array>
   53    12      > RETURN                                                   null

End of function __construct

Function generatehtml:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 17
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 17
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
filename:       /in/NhAke
function name:  generateHTML
number of ops:  40
compiled vars:  !0 = $image_width, !1 = $generated_path, !2 = $srcset
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   FETCH_STATIC_PROP_R          unknown             ~3      'image_widths'
          1      > FE_RESET_R                                       $4      ~3, ->17
          2    > > FE_FETCH_R                                               $4, !0, ->17
   20     3    >   INIT_METHOD_CALL                                         'generatePath'
          4        CHECK_FUNC_ARG                                           
          5        FETCH_OBJ_FUNC_ARG                               $5      'image_path'
          6        SEND_FUNC_ARG                                            $5
          7        SEND_VAR_EX                                              !0
          8        DO_FCALL                                      0  $6      
          9        ASSIGN                                                   !1, $6
   22    10        ROPE_INIT                                     4  ~10     !1
         11        ROPE_ADD                                      1  ~10     ~10, '+'
         12        ROPE_ADD                                      2  ~10     ~10, !0
         13        ROPE_END                                      3  ~9      ~10, 'w'
         14        ASSIGN_DIM                                               !2
         15        OP_DATA                                                  ~9
   19    16      > JMP                                                      ->2
         17    >   FE_FREE                                                  $4
   25    18        INIT_FCALL                                               'sprintf'
   28    19        SEND_VAL                                                 '%3Cdiv+class%3D%22%25s%22%3E%3Cimg+sizes%3D%22%25s%22+srcset%3D%22%25s%22+src%3D%22%25s%22+alt%3D%22%25s%22+%2F%3E%3C%2Fdiv%3E'
   29    20        FETCH_STATIC_PROP_R          unknown             ~12     'class'
         21        SEND_VAL                                                 ~12
   30    22        FETCH_STATIC_PROP_R          unknown             ~13     'sizes'
         23        SEND_VAL                                                 ~13
   31    24        INIT_FCALL                                               'implode'
         25        SEND_VAL                                                 '%2C'
         26        SEND_VAR                                                 !2
         27        DO_ICALL                                         $14     
         28        SEND_VAR                                                 $14
   32    29        INIT_FCALL                                               'end'
         30        SEND_REF                                                 !2
         31        DO_ICALL                                         $15     
         32        SEND_VAR                                                 $15
   33    33        FETCH_OBJ_R                                      ~16     'alt'
         34        SEND_VAL                                                 ~16
   25    35        DO_ICALL                                         $17     
   33    36        VERIFY_RETURN_TYPE                                       $17
         37      > RETURN                                                   $17
   35    38*       VERIFY_RETURN_TYPE                                       
         39*     > RETURN                                                   null

End of function generatehtml

Function generatepath:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/NhAke
function name:  generatePath
number of ops:  11
compiled vars:  !0 = $image_path, !1 = $arr_pos
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   40     2        INIT_FCALL                                               'str_replace'
          3        SEND_VAL                                                 '%5B-IMAGE_SIZE-%5D'
          4        SEND_VAR                                                 !1
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $2      
          7        VERIFY_RETURN_TYPE                                       $2
          8      > RETURN                                                   $2
   41     9*       VERIFY_RETURN_TYPE                                       
         10*     > RETURN                                                   null

End of function generatepath

End of class Image_P_Square.

Class Project:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 13, Position 2 = 18
Branch analysis from position: 13
2 jumps found. (Code = 78) Position 1 = 14, Position 2 = 18
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
filename:       /in/NhAke
function name:  __construct
number of ops:  20
compiled vars:  !0 = $link, !1 = $title, !2 = $description, !3 = $images, !4 = $image
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   65     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV_INIT                                        !3      <array>
   71     4        ASSIGN_OBJ                                               'link'
          5        OP_DATA                                                  !0
   72     6        ASSIGN_OBJ                                               'title'
          7        OP_DATA                                                  !1
   73     8        ASSIGN_OBJ                                               'description'
          9        OP_DATA                                                  !2
   75    10        ASSIGN_OBJ                                               'images'
         11        OP_DATA                                                  <array>
   77    12      > FE_RESET_R                                       $9      !3, ->18
         13    > > FE_FETCH_R                                               $9, !4, ->18
   78    14    >   INIT_METHOD_CALL                                         'addImage'
         15        SEND_VAR_EX                                              !4
         16        DO_FCALL                                      0          
   77    17      > JMP                                                      ->13
         18    >   FE_FREE                                                  $9
   80    19      > RETURN                                                   null

End of function __construct

Function generatehtml:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 8
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 8
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
filename:       /in/NhAke
function name:  generateHTML
number of ops:  23
compiled vars:  !0 = $imageHTML, !1 = $image
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   84     0  E >   ASSIGN                                                   !0, ''
   86     1        FETCH_OBJ_R                                      ~3      'images'
          2      > FE_RESET_R                                       $4      ~3, ->8
          3    > > FE_FETCH_R                                               $4, !1, ->8
   87     4    >   INIT_METHOD_CALL                                         !1, 'generateHTML'
          5        DO_FCALL                                      0  $5      
          6        ASSIGN_OP                                     8          !0, $5
   86     7      > JMP                                                      ->3
          8    >   FE_FREE                                                  $4
   90     9        INIT_FCALL                                               'sprintf'
  103    10        SEND_VAL                                                 '%3Carticle+class%3D%22project-single-container%22+data-project%3D%22%25s%22%3E%3Cdiv+class%3D%22project-single%22%3E%3Cdiv+class%3D%22project-header%22%3E%3Ca+class%3D%22project-details%22%3EDetails%3C%2Fa%3E%3Ch2+class%3D%22project-title%22%3E%25s%3C%2Fh2%3E%3Ca+class%3D%22project-close%22%3EClose%3C%2Fa%3E%3C%2Fdiv%3E%3Cdiv+class%3D%22project-content%22%3E%3Cdiv+class%3D%22project-details-inner%22%3E%25s%3C%2Fdiv%3E%3Cdiv+class%3D%22images-wrapper%22%3E%25s%3C%2Fdiv%3E%3C%2Fdiv%3E%3C%2Fdiv%3E%3C%2Farticle%3E'
  104    11        FETCH_OBJ_R                                      ~7      'link'
         12        SEND_VAL                                                 ~7
  105    13        FETCH_OBJ_R                                      ~8      'title'
         14        SEND_VAL                                                 ~8
  106    15        FETCH_OBJ_R                                      ~9      'description'
         16        SEND_VAL                                                 ~9
  107    17        SEND_VAR                                                 !0
   90    18        DO_ICALL                                         $10     
  107    19        VERIFY_RETURN_TYPE                                       $10
         20      > RETURN                                                   $10
  109    21*       VERIFY_RETURN_TYPE                                       
         22*     > RETURN                                                   null

End of function generatehtml

Function addimage:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/NhAke
function name:  addImage
number of ops:  5
compiled vars:  !0 = $image
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  111     0  E >   RECV                                             !0      
  113     1        FETCH_OBJ_W                                      $1      'images'
          2        ASSIGN_DIM                                               $1
          3        OP_DATA                                                  !0
  114     4      > RETURN                                                   null

End of function addimage

End of class Project.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
158.52 ms | 1110 KiB | 17 Q