Programacion Orientada a PHP Objetos (POO)

10
Programacion Orientada a PHP Objetos (POO) Eliseo Melgarejo http :// www.udec.cl / ~eliseomelgarejo Daniel Mahn http :// www.udec.cl / ~dmahn Desarrollo de sistemas Udec

description

Desarrollo de sistemas Udec. Programacion Orientada a PHP Objetos (POO). Eliseo Melgarejo http://www.udec.cl/~eliseomelgarejo. Daniel Mahn http://www.udec.cl/~dmahn. ¿Qué Es POO?. POO _ ejemplo1.

Transcript of Programacion Orientada a PHP Objetos (POO)

Page 1: Programacion Orientada a                 PHP Objetos (POO)

ProgramacionOrientada a PHPObjetos (POO)

Eliseo Melgarejohttp://www.udec.cl/~eliseomelgarejo

Daniel Mahn http://www.udec.cl/~dmahn

Desarrollo de sistemasUdec

Page 2: Programacion Orientada a                 PHP Objetos (POO)

¿Qué Es POO?

Page 3: Programacion Orientada a                 PHP Objetos (POO)

POO _ ejemplo1

<?

$nombre = "cc"; $obj = new $nombre;

class cc { function __construct() { echo 'holaaaaaaaaa!'; } }

?>

Page 4: Programacion Orientada a                 PHP Objetos (POO)

POO _ classe caja

<?

class Caja{var $contenido;

function introduce($cosa){$this->contenido = $cosa;}

function muestra_contenido(){echo $this->contenido;}

function vacia_caja(){$this->introduce("polvo");}

}

$micaja = new Caja();$micaja->introduce("algo");$micaja->muestra_contenido();

echo "<br>";

$segunda_caja = $micaja;$segunda_caja->introduce("contenido en segunda caja");$segunda_caja->muestra_contenido();

echo "<br>";

$micaja->muestra_contenido();

echo "<br>";$micaja->vacia_caja();$micaja->muestra_contenido();echo "<br>";

?>

Page 5: Programacion Orientada a                 PHP Objetos (POO)

POO _ classe auto

<?

class auto { var $llantas; var $color; var $velocidad;

public function acelerar($velocidad) { $this->velocidad += $velocidad; return $velocidad; }

public function frenar($velocidad) { $this->velocidad -= $velocidad; return $velocidad; }

public function parar( ) { $this->velocidad = 0; }

public function mostrar() { echo "$this->color <br>"; echo "El auto tiene $this->llantas"; } }

$car = new auto(); $car->color="rojo"; $car->llantas=4; $car->mostrar(); ?>

Page 6: Programacion Orientada a                 PHP Objetos (POO)

POO _ coordenadas

<?php

function pinta_tabla($x, $y) { $t0 = microtime(TRUE); echo "<table border='1'>"; for ($i=0; $i<$x; $i++) { echo "<tr>"; for ($j=0; $j<$y; $j++) { echo "<td> ($i, $j) </td>"; } echo "</tr>"; } echo "</table>"; $t1 = microtime(TRUE); echo "<p>Tiempo empleado: " . ($t1 - $t0)*1000 . "</p>"; }

pinta_tabla($_GET['x'], $_GET['y']); ?>

Page 7: Programacion Orientada a                 PHP Objetos (POO)

POO - coordenadas <?php

class Tabla { var $x, $y; function __construct($x, $y) { $this->x = $x; $this->y = $y; } function pinta_tabla() { $t0 = microtime(TRUE); echo "<table border='1'>"; for ($i=0; $i<$this->x; $i++) { echo "<tr>"; for ($j=0; $j<$this->y; $j++) { echo "<td> ($i, $j) </td>"; } echo "</tr>"; } echo "</table>"; $t1 = microtime(TRUE); echo "<p>Tiempo empleado: " . ($t1 - $t0)*1000 . "</p>"; } }

$tabla = new Tabla($_GET['x'], $_GET['y']); $tabla->pinta_tabla();

?>

Page 8: Programacion Orientada a                 PHP Objetos (POO)

POO->idiomas->procesar0.php

<html> <head> <title>Archivo index.php</title> </head> <body>

<? if(isset($error) && $error == true){ echo "Lo sentimos ha ocurrido un error al procesar tu idioma"; unset($error); //Removemos la variable de error } ?> <form action="procesar.php" method="post"> <select name="idioma"> <option value="ingles">English</option> <option value="espanol">Español</option> </select>

<!-- Creamos un campo escondido para decirle a la clase que funcion debe de tomar --> <input type="hidden" name="subenviar" value="1">

<!-- Creamos el boton para enviar --> <input type="submit" value="Enviar">

</form> </body> </html>

Page 9: Programacion Orientada a                 PHP Objetos (POO)

<?

ob_start(); //para usar el header

class Procesar{function Procesar(){ if(isset($_POST['subenviar']))

{

$this->elegirIdioma(); } else{ $error = true; return $error; header("Location: index.php"); } //Termina el constructor }

function elegirIdioma(){

if($_POST['idioma'] == "ingles"){ $mensaje = "Hello World"; header("Location: mostrar.php?mensaje=$mensaje");

} else{

$mensaje = "Hola mundo"; header("Location: mostrar.php?mensaje=$mensaje"); } } }$obj = new Procesar();?>

POO->idiomas->procesar.php

Page 10: Programacion Orientada a                 PHP Objetos (POO)

<html><head> <title>Archivo mostrar.php</title></head><body><?

if(isset($_GET['mensaje']))

{ echo $_GET['mensaje']; unset($mensaje);

} else{ echo "Ha ocurrido un error desconocido, porfavor da clic atras e intenta de nuevo"; }?></body></html>

POO->idiomas->mostrar.php