class Aula {
//propiedades nombre de tipo string y capacidad de tipo entero privadas
//Crear constructor con nombre obligatorio y capacidad opcional (por defecto 15)
//Crear getters y setters: El nombre no puede estar vacío y la capacidad entre 10 y 100
private $nombre;
private $capacidad;
function __construct(string $nombre, int $capacidad = 15) {
$this->setNombre($nombre);
$this->setCapacidad($capacidad);
}
function getNombre() {
return $this->nombre;
}
function getCapacidad() {
return $this->capacidad;
}
function setNombre(string $nombre) {
if (!empty($nombre)) {
$this->nombre = $nombre;
} else {
throw new Exception('El nombre no puede estar vacío');
}
}
function setCapacidad(int $capacidad) {
if ($capacidad < 10 || $capacidad > 100) {
throw new Exception('Capacidad entre 10 y 100');
} else {
$this->capacidad = $capacidad;
}
}
}
try {
$a = new Aula("Nombre");
$a->setCapacidad(500);
} catch (Exception $ex) {
echo $ex->getMessage() . " en la línea " . $ex->getLine();
}
Categoría: PHP
Ejemplo herencia en PHP
class Producto {
public $nombre;
public $precio;
function __construct($nombre, $precio = 0) {
$this->nombre = $nombre;
$this->precio = $precio;
}
function resumen() {
return $this->nombre . " " . $this->precio;
}
function pvp() {
return $this->precio * 1.21;
}
}
class Cd extends Producto {
public $longitud;
}
$nuevoCd=new Cd('Reggeton mix');
$nuevoCd->longitud=90;
$nuevoCd->precio=20;
var_dump($nuevoCd);
//Crear una clase libro con propiedad páginas que derive de producto
class Libro extends Producto{
public $paginas;
//Sobreescribir el constructor añadiendo el parámetro páginas
function __construct($nombre, $paginas, $precio = 0) {
parent::__construct($nombre, $precio);
$this->paginas=$paginas;
}
//Sobreescribimos el método de la clase madre
function resumen(){
//Invocamos el método de la clase madre
return parent::resumen()." ".$this->paginas;
}
}
class LibroAntiguo extends Libro{
public $anyo;
function __construct($nombre, $paginas,$anyo, $precio = 0) {
parent::__construct($nombre, $paginas, $precio);
$this->anyo=$anyo;
}
}
$nuevoLibro=new Libro('El Quijote',400);
echo $nuevoLibro->resumen();
$antiguo=new LibroAntiguo('Quijote',500,1754);
echo $antiguo->resumen();
Expresiones regulares
Manual oficial php:
http://php.net/manual/es/reference.pcre.pattern.syntax.php
Manuales completos:
https://didesweb.com/tutoriales-php/expresiones-regulares-php/
https://diego.com.es/expresiones-regulares-en-php
http://meudiseny.com/miphp/ejmExpreRegul.php
En esta página podemos probar expresiones regulares y ver si funcionan:
Clase genérica BD
class BD {
static $server = "localhost";
static $user = "root";
static $password = "";
static $database = "sakila";
private $table;
private $idField;
private $fields;
private $showFields;
static private $conn;
public function __construct($table, $idField, $fields = "", $showFields = "") {
$this->table = $table;
$this->idField = $idField;
$this->fields = $fields;
$this->showFields = $showFields;
self::conectar();
}
static function conectar() {
try {
self::$conn = new PDO("mysql:host=" . self::$server . ";dbname=" . self::$database, self::$user, self::$password, [PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"]);
self::$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $ex) {
echo $ex->getMessage();
}
}
function __get($name) {
if (property_exists($this, $name)) {
return $this->$name;
}
}
function __set($name, $value) {
if (property_exists($this, $name) && !empty($value)) {
$this->$name = $value;
} else {
throw new Exception("Error: datos incorrectos");
}
}
function getAll($condicion = "") {
$where = "";
if (!empty($condicion)) {
//Aquí tendré que hacer algo!!!
$where = " where 1=1 ";
foreach ($condicion as $clave => $valor) {
$where .= " and " . $clave . " = '" . $valor . "' ";
}
}
$res = self::$conn->query("select * from " . $this->table . $where);
return $res->fetchAll(PDO::FETCH_ASSOC);
}
function getAllPrepare($condicion = []) {
$where = "";
if (!empty($condicion)) {
$where = " where ". join(" and ", array_map(function($v) {
return $v . "=:" . $v;
}, array_keys($condicion)));
}
$st = self::$conn->prepare("select * from " . $this->table . $where);
$st->execute($condicion);
return $st->fetchAll(PDO::FETCH_ASSOC);
}
/**
* Esta función nos devuelve el elemento de la tabla que tenga este id
* @param int $id El id de la fila
*/
function getById($id) {
$res = self::$conn->query("select * from " . $this->table . " where "
. $this->idField . "=" . $id);
return $res->fetch();
}
/**
* Elimina el registro que tenga el id que le pasamos
* @param int $id
*/
function deleteById($id) {
try {
self::$conn->exec("delete from " . $this->table . " where "
. $this->idField . "=" . $id);
} catch (Exception $ex) {
echo $ex->getMessage();
}
}
/**
* Esta función toma como parámetro un array asociativo y nos inserta en la tabla
* un registro donde la clave del array hace referencia al campo de la tabla y
* el valor del array al valor de la tabla.
* ejemplo para la tabla actor: insert(['first_name'=>'Ana','last_name'=>'Pi'])
* @param type $valores
*/
function insert($valores) {
try {
$campos = join(",", array_keys($valores));
$parametros = ":" . join(",:", array_keys($valores));
$sql = "insert into " . $this->table . "($campos) values ($parametros)";
$st = self::$conn->prepare($sql);
$st->execute($valores);
} catch (Exception $ex) {
echo $ex->getMessage();
}
}
/**
* Modifica el elemento de la base de datos con el id que pasamos
* Con los valores del array asociativo
* @param int $id Id del elemento a modificar
* @param array $valores Array asociativo con los valores a modificar
*/
function update($id, $valores) {
try {
//Easy way
$res = [];
foreach ($valores as $clave => $valor) {
$res[] = $clave . "=:" . $clave;
}
$campos = join(",", $res);
//Hack way
$campos = join(",", array_map(function($v) {
return $v . "=:" . $v;
}, array_keys($valores)));
$sql = "update " . $this->table . " set " . $campos . " where "
. $this->idField . " = " . $id;
$st = self::$conn->prepare($sql);
$st->execute($valores);
} catch (Exception $ex) {
echo $ex->getMessage();
}
}
}
Clase BD de acceso a datos
<?php
class BD {
static $server = "localhost";
static $user = "root";
static $password = "";
static $database = "sakila";
private $table;
static private $conn;
public function __construct($table) {
$this->table = $table;
self::conectar();
}
static function conectar() {
try {
self::$conn = new PDO("mysql:host=" . self::$server . ";dbname=" . self::$database, self::$user, self::$password, [PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"]);
self::$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $ex) {
echo $ex->getMessage();
}
}
function getAll() {
$res = self::$conn->query("select * from " . $this->table);
return $res->fetchAll();
}
/**
* Esta función nos devuelve el elemento de la tabla que tenga este id
* @param int $id El id de la fila
*/
function getById($id) {
$res = self::$conn->query("select * from " . $this->table . " where "
. $this->table . "_id=" . $id);
return $res->fetch();
}
/**
* Elimina el registro que tenga el id que le pasamos
* @param int $id
*/
function deleteById($id) {
try {
self::$conn->exec("delete from " . $this->table . " where "
. $this->table . "_id=" . $id);
} catch (Exception $ex) {
echo $ex->getMessage();
}
}
/**
* Esta función toma como parámetro un array asociativo y nos inserta en la tabla
* un registro donde la clave del array hace referencia al campo de la tabla y
* el valor del array al valor de la tabla.
* ejemplo para la tabla actor: insert(['first_name'=>'Ana','last_name'=>'Pi'])
* @param type $valores
*/
function insert($valores) {
try {
$campos = join(",", array_keys($valores));
$parametros = ":" . join(",:", array_keys($valores));
$sql = "insert into " . $this->table . "($campos) values ($parametros)";
$st = self::$conn->prepare($sql);
$st->execute($valores);
} catch (Exception $ex) {
echo $ex->getMessage();
}
}
}
$actores = new BD("actor");
//print_r($actores->getAll());
$paises = new BD("country");
//$paises->insert(['country'=>'Chiquitistán']);
//print_r($paises->getAll());
//print_r($actores->getById(1));
$v = ['first_name' => 'Ana', 'last_name' => 'Pimiento'];
$actores->insert($v);
Clases y métodos finales
<?php
//Clases final
class Pepe {
public function saludo() {
echo "Hola";
}
final public function despedida(){
echo "Adios";
}
}
class Pepito extends Pepe {
public function saludo() {
echo "Hola a todo";
}
/* Esto da error
public function despedida(){
echo "Adios muy buenas";
}
*
*/
}
final class NoHeredable{
public $departamento;
}
//Esto da error
class QuieroHeredar extends NoHeredable{
}
$p=new Pepito();
?>
Recorrer propiedades objetos
<?php
//Recorrer objetos
class MiClase {
public $var1 = 'valor 1';
public $var2 = 'valor 2';
public $var3 = 'valor 3';
protected $protected = 'variable protegida';
private $private = 'variable privada';
function iterateVisible() {
echo "MiClase::iterateVisible:<br/>";
foreach ($this as $clave => $valor) {
echo $clave." - ".$valor."<br/>";
}
}
}
$clase=new MiClase();
foreach($clase as $clave=>$valor){
echo $clave." - ".$valor."<br/>";
}
$clase->iterateVisible();
?>
Rasgos (traits)
<?php
//Rasgos (traits)
trait Educado {
function saludo() {
echo "Hola";
}
function despedida() {
echo "adios";
}
}
trait HTML {
function cabecera() {
echo "<h1>$this->nombre</h1>";
}
}
class Pepe {
use Educado;
}
class Juan {
public $nombre;
use Educado,
HTML;
}
$obj = new Pepe;
$obj->despedida();
$obj2 = new Juan();
$obj2->nombre="Juan";
$obj2->despedida();
$obj2->cabecera();
?>
Interfaces
<?php
//Interfaces
interface webHTML{
public function dibujaTabla();
public function dibujaCabecera($nombre);
}
class miTabla implements webHTML{
public function dibujaTabla() {
echo "<table><tr><td></td></tr></table>";
}
public function dibujaCabecera($nombre) {
echo "<h1>$nombre</h1>";
}
public function saludo(){
echo "hola";
}
}
class OtraTabla implements webHTML{
public function dibujaTabla() {
echo "hola";
}
public function dibujaCabecera($nombre) {
echo $nombre;
}
}
?>
Clases Abstractas
<?php
//Clases abstractas
//No se puede instanciar
abstract class ClaseAbstracta {
public $nombre;
function saludo() {
echo "Hola";
}
abstract function despedida();
}
class ClaseConcreta extends ClaseAbstracta {
//Obligado a implementar despedida
function despedida() {
echo "adios";
}
}
//Da error porque no implementamos despedida
class OtraClase extends ClaseAbstracta{
}
$fallo=new ClaseAbstracta(); //ERROR
$obj = new ClaseConcreta();
$obj->nombre = "Juan";
$obj->saludo();