I have a book class that has among its attributes an array where the records of each loan that is made of the book (reader code, loan date, estimated return date and real return date) must be kept.
To do this, I first use the prestar()
function that creates an associative array for each parameter in the last position of the same, thus giving rise to a bidimensional array of loan records. The function prestar()
fills in all the fields of the loan record, except the last Freal
that must be filled by the function devolver()
and that's where the problem is, I use the function end()
to access the last field of the array but although I do not get an error and even doing echo
you can see that access to that last position does not fill in the field I'm missing.
final class libro extends publicacion implements prestable{
private $prestado = false;
private $ArrayPrestamos = array();
public function __get($atributo) {
if(property_exists(__CLASS__, $atributo)){
return $this->$atributo;
} else {
return null;
}
}
public function __set($nombre, $valor) {
$this->$nombre=$valor;
}
public function __toString(){
return parent::__toString()." | ¿Prestado? ".(boolval($this->prestado) ? 'Si' : 'No');
}
public function prestar($codLector) {
try{
if(!$this->prestado) {
$f_actual = date('d-m-Y');
$f_stimada = strtotime('+10 day' , strtotime($f_actual));
$f_stimada = date('d-m-Y' , $f_stimada);
$this->ArrayPrestamos[] = array ("codL"=>$codLector, "Fprestamo"=>$f_actual, "Fstimada"=>$f_stimada, "Freal"=>'');
$this->prestado = true;
} else {
throw new InvalidArgumentException("ERROR: El libro ya esta prestado");
}
}
catch(InvalidArgumentException $e){
echo $e->getMessage();
}
}
public function devolver() {
try{
if($this->prestado) {
$f_actual = date('d-m-Y');
end($this->ArrayPrestamos)["Freal"] = $f_actual;
$this->prestado = false;
} else {
throw new InvalidArgumentException("ERROR: El libro no esta prestado");
}
}
catch(InvalidArgumentException $e) {
echo $e->getMessage();
}
}
public function EstaPrestado() {
return $this->prestado;
}
public function MuestraArray() {
return var_dump($this->ArrayPrestamos);
}
}
And here is the proof of execution:
echo "<p>--- Creamos el libro \"Titulo Libro 1\"</p>";
$book1 = new libro (4, "Titulo Libro 1", 1997);
$book1->prestar(44);
$book1->devolver();
echo "<p>".$book1->MuestraArray()."</p>";
echo $book1;