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();
Output for git.master, git.master_jit, rfc.property-hooks
<article class="project-single-container" data-project="test_link"><div class="project-single"><div class="project-header"><a class="project-details">Details</a><h2 class="project-title">test_title</h2><a class="project-close">Close</a></div><div class="project-content"><div class="project-details-inner"><p>Test description</p></div><div class="images-wrapper"><div class="ratio-box r1-1"><img sizes="50vw" srcset="https://res.cloudinary.com/demo/image/upload/c_scale,w_192/lady.jpg 192w,https://res.cloudinary.com/demo/image/upload/c_scale,w_384/lady.jpg 384w,https://res.cloudinary.com/demo/image/upload/c_scale,w_576/lady.jpg 576w,https://res.cloudinary.com/demo/image/upload/c_scale,w_768/lady.jpg 768w" src="https://res.cloudinary.com/demo/image/upload/c_scale,w_768/lady.jpg 768w" alt="this is alt" /></div></div></div></div></article>

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
33.06 ms | 407 KiB | 5 Q