Advertisement
niwdeyen

Ejemplos Clases PHP

Nov 22nd, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. https://youtu.be/I26upu4ZOCI
  2. ----
  3. classuser.php
  4. ----
  5. <?php
  6. class User
  7. {
  8. public $name;
  9. public $age;
  10.  
  11. public function Describe()
  12. {
  13. return $this->name . " tiene " . $this->age . " años de edad";
  14. }
  15. }
  16. ?>
  17. ----
  18. classper.php
  19. ----
  20. <?php
  21. class Persona {
  22. public $nombre;
  23. public function asignarnombre($nom)
  24. {
  25. $this->nombre=$nom;
  26. }
  27. public function imprimir()
  28. {
  29. echo $this->nombre;
  30. echo '<br>';
  31. }
  32.  
  33.  
  34. }
  35. ?>
  36. ----
  37. p1.php
  38. ----
  39. <html>
  40. <head>
  41. <title>Pruebas</title>
  42. </head>
  43. <body>
  44. <?php
  45. include 'classper.php';
  46.  
  47.  
  48. error_reporting(E_ALL);
  49. ini_set("display_errors", 0);
  50.  
  51.  
  52. $per1=new Persona();
  53. $per1->asignarnombre('Pepe');
  54. $per1->imprimir();
  55. echo "<br>";
  56. echo $per1->$nombre;
  57. echo "<br>";
  58. $per2=new Persona();
  59. $per2->asignarnombre('Ana');
  60. $per2->imprimir();
  61.  
  62. ?>
  63. </body>
  64. </html>
  65.  
  66. ----
  67. p2.php
  68. ----
  69.  
  70. <html>
  71. <head>
  72. <title>Pruebas</title>
  73. </head>
  74. <body>
  75. <?php
  76. include 'classuser.php';
  77.  
  78. $user = new User();
  79. $user->name = "Eddie Smith";
  80. $user->age = 99;
  81. echo $user->Describe();
  82.  
  83. ?>
  84. </body>
  85. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement