Comunidad de diseño web y desarrollo en internet online

Clase Página con PHP Orientado a Objetos

Citar            
MensajeEscrito el 05 Jun 2007 01:52 pm
Hola gente acá les paso un ejemplo de una clase pagina. La verdad que es muy útil, para aquellos que reutilizan código un abrazo!!!! :cool:

Código :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Clase pagina Nro 2</title>
</head>

<body>
<?
class Pagina
{
////CAMARADAS ACÁ LES MANDO UN EJEMPLO MUY UTIL DE LA UTILIZACION DE ORIENTACION A OBJETOS
////CON PHP, ES UN CODIGO MUY REUTILIZABLE, SUERTE!!!!////////////////////////////////////
///////// ACÁ DECLARAMOS LOS ATRIBUTOS DE LA CLASE/////////////////////////
  var $contenido;
  var $titulo = "Multi Librería Online";
  var $palabrasClave = "Librería Online, Aquí pondremos las palabras clave,
                   Podemos personalizarlas para cada página";
  var $botones = array( "Inicio"     => "ARCHIVO.php",
                        "Contacto"  => "ARCHIVO.php",
                        "Servicios" => "ARCHIVO.php",
                        "Mapa Sitio" => "ARCHIVO.php"
                      );

//////// ACÁ DECLARAMOS LAS OPERACIONES DE LA CLASE///////////////////////////////
 
  function SetContenidos($nuevosContenidos)
  {
    $this->contenidos = $nuevosContenidos;
  }

  function SetTitulo($nuevoTitulo)
  {
    $this->titulo = $nuevoTitulo;
  }

  function SetPalabrasClave($nuevasPalabrasClave)
  {
    $this->palabrasClave = $nuevasPalabrasClave;
  }

  function SetBotones($nuevosBotones)
  {
    $this->botones = $nuevosBotones;
  }

  function Mostrar()
  {
    echo "<html>\n<head>\n";
    $this -> MostrarTitulo();  // LLAMO A LA FUNCION MOSTRAR TITULO
    $this -> MostrarPalabrasClave(); 
    $this -> MostrarEstilos();
    echo "</head>\n<body>\n";
    $this -> MostrarCabecera();
    $this -> MostrarMenu($this->botones);
    echo $this->contenidos;
    $this -> MostrarFooter();
    echo "</body>\n</html>\n";
  }

  function MostrarTitulo()
  {
    echo "<titulo> $this->titulo </titulo>";
  }

  function MostrarPalabrasClave()
  {
    echo "<META name=\"palabrasClave\" contenidos=\"$this->palabrasClave\">";
  }

  function MostrarEstilos()
  {
?>
<!-- ACA CODIFICAMOS NUESTRO ESTILO CSS-->
  <style> 
  body {
        background-color: #000000;
}
    h1 {color:white; font-size:12pt; text-align:center;
        font-family:arial,sans-serif}
    .menu {color:#FFCD6A; font-size:10pt; text-align:center;
           font-family:arial,sans-serif; font-weight:bold}
    td {background:black}
    p {color:white; font-size:12pt; text-align:justify;
       font-family:arial,sans-serif}
    p.foot {color:white; font-size:9pt; text-align:center;
            font-family:arial,sans-serif; font-weight:bold}
    a:link,a:visited,a:active {color:white}
  </style>
<!-- FIN DE LA CODIFICACIÓN CSS-->  
  
<? // ACA HABRIMOS CODIGO PHP UNICAMENTE PARA DECLARAR LA FUNCION MOSTRAR CABECERA//
  
  }
  function MostrarCabecera()
  {

// ACA CERRAMOS EL CODIGO PHP PARA PARA REALIZARLA CABECERA EN HTML
?>
<table width="100%"  border="0" cellspacing="0" cellpadding="1">
  <tr>
    <th scope="col"><div align="center">
      <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="780" height="237">
          <param name="movie" value="0042.swf">
          <param name="quality" value="high">
          <embed src="0042.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="780" height="237"></embed>
      </object>
    </div></th>
  </tr>
</table>
<?

  }// ESTA LLAVE ES LA LLAVE DE CIERRE DE LA DECLARACIÓN DE LA FUNCION "MostrarCabecera", antes abierta/////////

  function MostrarMenu($botones)
  {
    echo "<table width = \"100%\" bgcolor = white cellpadding = 4 cellspacing = 4>\n";
    echo "  <tr>\n";

    //ACÁ SE CALCULA EL TAMAÑO DE LOS BOTONES, YA QUE ESTA CLASE NOS PERMITE AGREGAR BOTONES SEGUN LAS 
   //LAS NECESIDADES DEL CASO. SE CUENTAN LOS BOTONES Y SE LOS DIVIDE EN UN ESPACIO DE 100,CON ESTO OBTENEMOS 
   //EL ANCHO EFECTIVO DE PAGINA PARA LOS BOTONES
       $width = 100/count($botones);

    while (list($nombre, $url) = each($botones))
    {
      $this -> MostrarBoton($width, $nombre, $url, !$this->IsURLCurrentPage($url));
    }
    echo "  </tr>\n";
    echo "</table>\n";
  }

  function IsURLCurrentPage($url)
  {
    if(strpos( $GLOBALS["SCRIPT_NAME"], $url )==false)
    {
      return false;
    }
    else
    {
      return true;
    }
  }

  function MostrarBoton($width, $nombre, $url, $activo = true)
  {
    if ($activo)
    {
      echo "<td width = \"$width%\">
            <a href = \"$url\">
            <img src = \"IMAGEN.gif\" alt = \"$nombre\" border = 0></a>
            <a href = \"$url\"><span class=menu>$nombre</span></a></td>";
    }
    else
    {
      echo "<td width = \"$width%\">
            <img src = \"IMAGEN.gif\">
            <span class=menu>$nombre</span></td>";
    }
  }

  function MostrarFooter() // CON ESTA FUNCION GENERAMOS EL PIE DE LA PAGINA//
  {
?>
   <body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<table id="Tabla_01" width="100%" height="51" border="0" cellpadding="0" cellspacing="0">
        <tr>
                <td>
                        <img src="Images.jpg" width="234" height="22" alt=""></td>
                <td>
                        <img src="Images.jpg" width="65" height="22" alt=""></td>
                <td>
                        <img src="Images.jpg" width="54" height="22" alt=""></td>
                <td>
                        <img src="Images.jpg" width="58" height="22" alt=""></td>
                <td>
                        <img src="Images.jpg" width="64" height="22" alt=""></td>
                <td>
                        <img src="Images.jpg" width="67" height="22" alt=""></td>
                <td>
                        <img src="Images.jpg" width="238" height="22" alt=""></td>
        </tr>
        <tr>
                <td colspan="7">
                        <img src="Images.jpg" width="100%" height="29" alt=""></td>
        </tr>
</table>
<!-- End ImageReady Slices -->
</body>
<?
  }
}
?>
<!--************ACÁ HAGO LA INSTANCIACION DE LA CLASE PAGINA************ -->
<?
 
  $paginaInicio = new Pagina();

  $paginaInicio -> SetContenidos("<table width=\"100%\"  border=\"0\" cellspacing=\"0\" cellpadding=\"1\">
  <tr>
    <th bgcolor=\"#000000\" scope=\"col\"><img src=\"Images/cuerpo.png\" width=\"780\" height=\"449\"></th>
  </tr>
</table>"
                         );
  $paginaInicio -> Mostrar();
?>
<!--****************************************************************** -->

</body>
</html>

Por PabloCss

6 de clabLevel



Genero:Masculino  

msie7
Citar            
MensajeEscrito el 05 Jun 2007 03:18 pm
Muy bien PabloCss, para ser un inicio en PHP y programación orientada a objetos. Sin embargo la mejor forma es separar el HTML del php. Te recomiendo este tutorial del Sr. Maikel.

http://www.cristalab.com/tutoriales/160/galeria-de-imagenes-con-templates-en-php


Saludos ^^

Por Dano

BOFH

4273 de clabLevel

14 tutoriales
4 articulos
10 ejemplos

Genero:Masculino   Bastard Operators From Hell Premio_Secretos

Lugar estratégico para vigilarte

firefox

 

Cristalab BabyBlue v4 + V4 © 2011 Cristalab
Powered by ClabEngines v4, HTML5, love and ponies.