Código PHP:
<?php
class html {
protected $html;
function __construct($html){
$this->setHtml($html);
}
function __tostring(){
return $this->html;
}
function setHtml($html){
$this->html =$html;
}
} #
/* decorator */
abstract class tag extends html {
protected $obj;
function __construct ($obj=null){
$this->obj = $obj;
}
} #
class link extends tag {
private $uri;
function __construct ($x=null){
if ($x instanceof tag){
parent::__construct($x);
}else{
$y = new html($x);
parent::__construct($x);
return $y;
}
}
function setUri($uri){
$this->uri = $uri;
}
function linkear (){
$this->setHtml ("<a href='{$this->uri}'>{$this->obj}</a>");
}
} #
class strong extends tag {
function render(){
$this->setHtml ('<strong>'.$this->obj.'</strong>');
}
} #
class italic extends tag {
function render(){
$this->setHtml ('<i>'.$this->obj.'</i>');
}
} #
class img extends html {
private $img;
function __construct($img){
$this->img = $img;
$this->render();
}
private function render(){
$this->setHtml ("<img src='{$this->img}' />");
}
} #
/* Creo HTML */
//$x = new html('Google');
/* Creo link sobre el HTML on_the_fly */
$h = new link('Google');
$h->setUri('http://google.es');
$h->linkear();
/* Le hago un strong */
$h = new strong ($h);
$h->render();
/* Le hago italica */
$h = new italic ($h);
$h->render();
echo $h;
/* Creo imagen y le aplico enlace */
$i = new img ('http://2.bp.blogspot.com/-nURw-T8ErDM/ThYBg4eyhGI/AAAAAAAAB8I/Tapme43aapE/s72-c/google-picasa-blogger.jpg');
$i = new link($i);
$i->setUri('http://google.es');
$i->linkear();
echo $i;
Cita:
GRACIAS <i><strong><a href='http://www.google.com'>Google</a></strong></i>