3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Libro { public $titulo; public $autor; public $precio; public function __constructor($titulo,$autor,$precio){ $this->titulo = $titulo; $this->autor = $autor; $this->precio = $precio; } public function getTitulo(){ return $this-> titulo; } public function getPrecio(){ return $this->precio; } public function setPrecio($nuevoPrecio){ if ($precio >= 0){ $this->precio = $nuevoPrecio; }else{ echo "Error: Debes de escribir algo" } } $libro1 = new Libro("Harry Potter", "J.K. Rowling", 20); // $libro1->precio = -10; // ESTO AHORA DARÍA ERROR FATAL (No puedes acceder a propiedad privada) // Forma correcta de intentar cambiar el precio: echo "Intentando poner precio negativo...<br>"; $libro1->setPrecio(-50); // El setter bloqueará esto echo "Intentando poner precio correcto...<br>"; $libro1->setPrecio(15); // El setter aceptará esto echo "<hr>"; echo $libro1->mostrarInfo(); // Para leer un dato suelto: echo "El precio actual es: " . $libro1->getPrecio() . "€"; public function mostrarInfo() { return "Libro: {$this->titulo}, escrito por {$this->autor}. Precio: ${$this->precio}"; } const $libro2 = new Libro("1984", "George Orwell", 15); echo $libro1->mostrarInfo(); echo $libro2->mostrarInfo(); }
Output for git.master, git.master_jit
Parse error: syntax error, unexpected token "}", expecting "," or ";" in /in/eahS6 on line 27
Process exited with code 255.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
138.49 ms | 1085 KiB | 7 Q