3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Libro { // 1. Ahora son PRIVATE (Solo la propia clase puede tocarlas) private $titulo; private $autor; private $precio; public function __construct($titulo, $autor, $precio) { $this->titulo = $titulo; $this->autor = $autor; // Usamos el setter interno para validar el precio inicial también $this->setPrecio($precio); } // --- GETTERS (Obtener info) --- public function getTitulo() { return $this->titulo; } public function getPrecio() { return $this->precio; } // --- SETTERS (Modificar info con reglas) --- public function setPrecio($nuevoPrecio) { // Aquí está la MAGIA: Validación if ($nuevoPrecio >= 0) { $this->precio = $nuevoPrecio; } else { echo "❌ Error: El precio no puede ser negativo ($nuevoPrecio). Se mantiene el anterior.<br>"; } } public function mostrarInfo() { // Como estamos DENTRO de la clase, sí podemos acceder a $this->precio directamente return "📖 Libro: {$this->titulo}, Autor: {$this->autor}, Precio: {$this->precio}€<br>"; } } // --- ZONA DE PRUEBAS --- $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() . "€"; ?>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oQa8Y
function name:  (null)
number of ops:  24
compiled vars:  !0 = $libro1
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   NEW                                              $1      'Libro'
          1        SEND_VAL_EX                                              'Harry+Potter'
          2        SEND_VAL_EX                                              'J.K.+Rowling'
          3        SEND_VAL_EX                                              20
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !0, $1
   50     6        ECHO                                                     'Intentando+poner+precio+negativo...%3Cbr%3E'
   51     7        INIT_METHOD_CALL                                         !0, 'setPrecio'
          8        SEND_VAL_EX                                              -50
          9        DO_FCALL                                      0          
   53    10        ECHO                                                     'Intentando+poner+precio+correcto...%3Cbr%3E'
   54    11        INIT_METHOD_CALL                                         !0, 'setPrecio'
         12        SEND_VAL_EX                                              15
         13        DO_FCALL                                      0          
   56    14        ECHO                                                     '%3Chr%3E'
   57    15        INIT_METHOD_CALL                                         !0, 'mostrarInfo'
         16        DO_FCALL                                      0  $6      
         17        ECHO                                                     $6
   59    18        INIT_METHOD_CALL                                         !0, 'getPrecio'
         19        DO_FCALL                                      0  $7      
         20        CONCAT                                           ~8      'El+precio+actual+es%3A+', $7
         21        CONCAT                                           ~9      ~8, '%E2%82%AC'
         22        ECHO                                                     ~9
   61    23      > RETURN                                                   1

Class Libro:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oQa8Y
function name:  __construct
number of ops:  11
compiled vars:  !0 = $titulo, !1 = $autor, !2 = $precio
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   11     3        ASSIGN_OBJ                                               'titulo'
          4        OP_DATA                                                  !0
   12     5        ASSIGN_OBJ                                               'autor'
          6        OP_DATA                                                  !1
   14     7        INIT_METHOD_CALL                                         'setPrecio'
          8        SEND_VAR_EX                                              !2
          9        DO_FCALL                                      0          
   15    10      > RETURN                                                   null

End of function __construct

Function gettitulo:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oQa8Y
function name:  getTitulo
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   FETCH_OBJ_R                                      ~0      'titulo'
          1      > RETURN                                                   ~0
   20     2*     > RETURN                                                   null

End of function gettitulo

Function getprecio:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oQa8Y
function name:  getPrecio
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   FETCH_OBJ_R                                      ~0      'precio'
          1      > RETURN                                                   ~0
   24     2*     > RETURN                                                   null

End of function getprecio

Function setprecio:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 6
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oQa8Y
function name:  setPrecio
number of ops:  11
compiled vars:  !0 = $nuevoPrecio
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   RECV                                             !0      
   30     1        IS_SMALLER_OR_EQUAL                                      0, !0
          2      > JMPZ                                                     ~1, ->6
   31     3    >   ASSIGN_OBJ                                               'precio'
          4        OP_DATA                                                  !0
   30     5      > JMP                                                      ->10
   33     6    >   ROPE_INIT                                     3  ~4      '%E2%9D%8C+Error%3A+El+precio+no+puede+ser+negativo+%28'
          7        ROPE_ADD                                      1  ~4      ~4, !0
          8        ROPE_END                                      2  ~3      ~4, '%29.+Se+mantiene+el+anterior.%3Cbr%3E'
          9        ECHO                                                     ~3
   35    10    > > RETURN                                                   null

End of function setprecio

Function mostrarinfo:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oQa8Y
function name:  mostrarInfo
number of ops:  12
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E >   ROPE_INIT                                     7  ~4      '%F0%9F%93%96+Libro%3A+'
          1        FETCH_OBJ_R                                      ~0      'titulo'
          2        ROPE_ADD                                      1  ~4      ~4, ~0
          3        ROPE_ADD                                      2  ~4      ~4, '%2C+Autor%3A+'
          4        FETCH_OBJ_R                                      ~1      'autor'
          5        ROPE_ADD                                      3  ~4      ~4, ~1
          6        ROPE_ADD                                      4  ~4      ~4, '%2C+Precio%3A+'
          7        FETCH_OBJ_R                                      ~2      'precio'
          8        ROPE_ADD                                      5  ~4      ~4, ~2
          9        ROPE_END                                      6  ~3      ~4, '%E2%82%AC%3Cbr%3E'
         10      > RETURN                                                   ~3
   40    11*     > RETURN                                                   null

End of function mostrarinfo

End of class Libro.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
147.87 ms | 1021 KiB | 13 Q