selimppc

ArrayObject.php

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