selimppc

PHP's ArrayObject class.

Jul 8th, 2015
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. <?php
  2.  
  3. class View
  4. {
  5. //definition
  6. private $id;
  7. private $name;
  8. private $email;
  9.  
  10. /*
  11. * Constructor
  12. */
  13. public function __construct($id, $name, $email)
  14. {
  15. $this->id = $id;
  16. $this->name = $name;
  17. $this->email = $email;
  18. }
  19.  
  20. /*
  21. * get ID
  22. */
  23. public function getId()
  24. {
  25. return $this->id;
  26. }
  27.  
  28. /*
  29. * get Name
  30. */
  31. public function getName()
  32. {
  33. return $this->name;
  34. }
  35.  
  36. /*
  37. * get Email
  38. */
  39. public function getEmail()
  40. {
  41. return $this->email;
  42. }
  43. }
  44.  
  45.  
  46. // New View List Class which extends arrayObject in PHP
  47. class ViewList extends ArrayObject
  48. {
  49. /*
  50. * a public function to return data
  51. */
  52. public function displayAsTable() // or you could even override the __toString if you want.
  53. {
  54. $sOutput = '<table border="1"><tbody>';
  55. foreach ($this AS $user)
  56. {
  57. $sOutput .= sprintf('<tr><td>%s</td><td>%s</td><td>%s</td></tr>',
  58. $user->getId(),
  59. $user->getName(),
  60. $user->getEmail()
  61. );
  62. }
  63. $sOutput .= print '</tbody></table>';
  64.  
  65. return $sOutput;
  66. }
  67.  
  68. /*
  69. * return data to string
  70. */
  71. public function __toString()
  72. {
  73. return $this->displayAsTable();
  74. }
  75. }
  76.  
  77. /*
  78. * data(s)
  79. */
  80. $data = new ViewList();
  81. $data[] = new View(1, 'Selim Reza', '[email protected]');
  82. $data[] = new View(2, 'Half Way', '[email protected]');
  83.  
  84. /*
  85. * final output
  86. */
  87. print $data;
Advertisement
Add Comment
Please, Sign In to add comment