Ver Mensaje Individual
  #1 (permalink)  
Antiguo 29/11/2008, 19:59
Avatar de Italico76
Italico76
 
Fecha de Ingreso: abril-2007
Mensajes: 3.303
Antigüedad: 17 años, 1 mes
Puntos: 292
Mi primer objeto - critiquen!!!

Amigos: este es mi primer intento de hacer un objeto en cualquier lenguaje y quiero que critiquen el diseño, implementacion y demas

La idea es simplificar la forma de hacer enlaces de texto o enlaces a imagenes y evitar usar parametros opcionales y otros enredos.

A continuacion mis clases "link2text" y "link2graph"

Código PHP:
<? 
// Voy a intentar hacer la clase ENLACE (mi primer objeto)

class Img {  // clase img 
  
private $url
  private 
$alt
  private 
$border;    
   
  function 
__construct($url){ 
   
$this->url=$url;   
   
$this->setborder(0);
  } 
  
 function 
addAlt($alt){
   
$this->alt="alt='$alt'";
  }  
  
 function 
setBorder($border){
   
$this->border="border='$border'";
 } 

  function 
getImg(){
   return 
$this->url// aca habia un error (!)
 


 function 
getAlt(){
   return 
$this->alt;
 } 
  
 function 
show (){
  return 
"<img src='$this->url' $this->alt $this->border  />";           
 }     


abstract class 
link // super-clase no-instanciable
  
protected $url;   
  protected 
$title
  protected 
$target// relacionado con newPage()
    
 
function __construct($url){ 
   
$this->url=$url;        
 }     
  
 function 
addTitle($title){
   
$this->title="title='$title'";   
 }
 
 function 
newPage(){
   
$this->target="target ='_blank'";
  }  
 
 function 
getTarget(){
   return 
$this->target;
 }  
 
 function 
getUrl(){
   return 
$this->url;
 } 
}

class 
Link2text extends link{    // clase Link a texto
  
private $anchor
    
  function 
__construct($url,$anchor){ 
   
parent::__construct($url); 
   
$this->anchor $anchor
  } 

 function 
setAnchor($anchor){ // faltaba (!)
   
$this->anchor=$anchor;
 }

 function 
getAnchor(){  // tenia un error (!)
   
return $this->anchor;
 }
 
 function 
__toString() {  // 'metodo magico'  que genera una salida (me evito ponerle nombre a la funcion porque se ejecuta sola al hacer echo o print)
   
if (isset($this->title)){
     
$title=' '.$this->title;
   }else{     
     
$title=$this->title;
   }     
   return 
"<a href='$this->url'".$title."$this->target>$this->anchor</a>";
 } 
}

class 
Link2graph extends link{  // clase Link a Grafico  
  
private $imagen;
    
  function 
__construct($url,$img_url){ 
   
parent::__construct($url);    
   
$this->imagen = new img($img_url);    
  }
  
  function 
addAlt($alt){
   
$this->imagen->addAlt($alt); // uso el metodo add_alt de la clase IMG
  
}  
    
 function 
__toString (){       
   
$img $this->imagen->show();    
   
$title=$this->title;        
    return 
"<a href='$this->url'  $this->title $this->target >$img</a>";
 }   
}

?>}
Aca como la uso:

Código PHP:
<?php
Include "clases.inc";

$enlace = new link2text("http://www.betadir.com","directorio de blogs");    
$enlace->add_title("directorio de blogs");
echo 
$enlace->show()."<p/>"

$enlace2 = new link2graph("http://www.asesdeljuego.com","http://www.asesdeljuego.com/juegos/100/finalfight.jpg");    
$enlace2->add_alt("juegos flash");
$enlace2->add_title("Juegos de lucha");
$enlace2->set_border(0);
echo 
$enlace2->show(); 

?>
__________________
Salu2!

Última edición por Italico76; 09/07/2009 a las 10:32