Debuggar con NetBeans

En php.ini:

 

[XDebug]
zend_extension = "C:\xampp\php\ext\php_xdebug.dll"
;xdebug.profiler_append = 0
xdebug.profiler_enable = 1
;xdebug.profiler_enable_trigger = 0
;xdebug.profiler_output_dir = "C:\xampp\tmp"
;xdebug.profiler_output_name = "cachegrind.out.%t-%s"
xdebug.remote_enable = 1
xdebug.remote_handler = "dbgp"
xdebug.remote_host = "127.0.0.1"
xdebug.trace_output_dir = "C:\xampp\tmp"
xdebug.remote_port=9000

Ejercicio clase Empleado

Propiedades:

Nombre (pública)

Fecha Contrato (Privada)

Sueldo (Privada)

Constructor: Poner la fecha del contrato la de hoy

Funciones:

setSueldo($valor) Pone en sueldo el valor si está entre 600 y 3000

getSueldo() Nos devuelve el valor del sueldo

sueldoNeto() Nos devuelve el valor del sueldo menos el IRPF

IRPF() Nos devuelve el valor del IRPF (puede ser privada). Si el sueldo es entre 600 y 1000, 10%. Entre 1000 y 2000, 13%. Entre 2000 y 3000, 16%

Solución:

<?php

class empleado {

 public $nombre;
 private $fechaContrato;
 private $sueldo;

 function __construct() {
 $this->fechaContrato = date("d/m/Y");
 }

 function setSueldo($valor) {
 if ($valor >= 600 and $valor <= 3000) {
 $this->sueldo = $valor;
 }
 }

 function getSueldo() {
 return $this->sueldo;
 }

 private function irpf() {
 if ($this->getSueldo() <= 1000) {
 return .1;
 }
 if ($this->sueldo <= 2000) {
 return .13;
 }
 if ($this->sueldo <= 3000) {
 return .16;
 }
 return 0;
 }

 function sueldoNeto(){
 return $this->sueldo*(1-$this->irpf());
 }
}

$paco=new empleado();
$paco->setSueldo(1500);
echo $paco->sueldoNeto();

Programación orientada a objetos

<?php

class miClase {

 public $propiedad;

 function muestraContenido() {
 echo $this->propiedad;
 }

}

$obj1 = new miClase;
$obj2 = new miClase();
$obj1->propiedad = "Hola";
$obj2->propiedad = "Adios";

echo $obj1->propiedad;
$obj2->muestraContenido();

class Alumno {

 public $nombre;
 public $apellido;
 public $nota;
 private $cuota;

 function nombreCompleto() {
 echo $this->nombre . " " . $this->apellido;
 }

 function aprobado() {
 if ($this->nota >= 5) {
 return true;
 } else {
 return false;
 }
 }

 function cuota($valor) {
 if ($valor == 100 || $valor == 200 || $valor == 300) {
 $this->cuota = $valor;
 }
 }

 function anualidad() {
 return $this->cuota * 12;
 }

}

$juan = new Alumno();
$ana = new Alumno();
$juan->nombre = "Juan";
$juan->apellido = "Pérez";
$juan->nota = 6;
$juan->nombreCompleto();
if ($juan->aprobado()) {
 echo "Aprobado!!!";
}
$juan->cuota(200);
echo $juan->anualidad();

$ana->apellido = "Ana";

Soluciones examen 2

/*
 * * MySQLi procedural * MySQLi orientat a objectes * PDO
 * 
 * MySql MySqli PDO
 */


$sql = "insert into clientes (mail,password) values (:mail,:password)";
$st = $conn->prepare($sql);
$st->execute(array(':mail' => 'pepe@pepe.com', ':password' => '1234'));


$sql = "SELECT count(idproductos) as total FROM productos";
$st = $conn->prepare($sql);
$st->execute();
$row = $st->fetch();
echo "El número de productos es: " . $row['total'] . "<br/>";


$sql = "SELECT sum(importe) as total FROM pedido";
$st = $conn->prepare($sql);
$st->execute();
$row = $st->fetch();
echo "La suma de los importes de los pedidos es: " . $row['total'] . "<br/>";


$sql = "delete from productos where idproductos=:idproductos";
$st = $conn->prepare($sql);
$st->execute(array(':idproductos' => 5));

$sql = "SELECT mail FROM clientes";
$st = $conn->prepare($sql);
$st->execute();
while ($row = $st->fetch()) {
 echo $row['mail'] . "<br/>";
}

$sql = "SELECT mail FROM clientes where mail like '%@gmail.com'";
$st = $conn->prepare($sql);
$st->execute();
while ($row = $st->fetch()) {
 echo $row['mail'] . "<br/>";
}


$sql = "insert into clientes (mail, password) values (:mail, :password)";
$st = $conn->prepare($sql);
for ($i = 0; $i < 10; $i++)
 $st->execute(array(':mail' => 'test@test.com', ':password' => '1234'));

function import_total_IVA($idclient) {
 global $conn;
 try {
 $sql = "select sum(import*(1+iva)) as total from comandes join clients using (idclients) where idclients=:idclients";
 $stmt = $conn->prepare($sql);
 $stmt->execute(array(':idclients' => $idclient));
 $fila = $stmt->fetch();
 $total = $fila['total'];
 return $total;
 } catch (Exception $ex) {
 return 0;
 }
}

Soluciones examen 1

//56

function lamaslarga($cad1, $cad2) {
 if (strlen($cad1) > strlen($cad2)) {
 return $cad1;
 } else {
 return $cad2;
 }
}

include_once 'funciones.php';

$f = fopen(‘alumnos.txt ’, ‘r’);

$colores = array("rojo", "naranja", "amarillo", "verde", "cian", "azul", "violeta");

if ($resultado) {
 ?> <h1>Bien</h1> <?php } else { ?> <h2>Mal</h2> <?php
}

function invertir_cad($tabla) {
 for ($i = 0; $i < count($tabla); $i++) {
 $tabla2[$i] = strrev($tabla[$i]);
 } return $tabla2;
}

function tablero($numCeldas) {
 $num = 0;
 echo "<table border=1>";
 for ($i = 0; $i < $numCeldas; $i++) {
 echo "<tr>";
 for ($j = 0; $j < $numCeldas; $j++) {
 echo "<td>" . ($i + $j) . "</td>";
 } 
 echo "</tr>";
 } echo "</table>";
}