Guest User

Untitled

a guest
Sep 7th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.80 KB | None | 0 0
  1. Child Class editing Parent Class Property
  2. <?php
  3.  
  4. class page {
  5. var $ot;
  6. function begin(){
  7. $this->ot = '';
  8. }
  9. function finish(){
  10. echo $this->ot;
  11. }
  12.  
  13.  
  14. class forms extends page {
  15. function __construct($form_action, $form_type){
  16. $this->ot .= '<form action=' . $form_action . ' method=' . $form_type . ' />';
  17. }
  18. function create_input($type, $name){
  19. $this->ot .= '<input type="' . $type . '" name="' . $name . '" /><br />';
  20. }
  21. function create_submit($value){
  22. $this->ot .= '<input type="submit" value="' . $value . '" />';
  23. }
  24. function __destruct(){
  25. $this->ot .= '</form>';
  26. }
  27. }
  28.  
  29. class labels extends page {
  30. function create_label($label){
  31. $this->ot .= '<label>' . $label . ' </label>';
  32. }
  33. }
  34.  
  35. $page = new page();
  36. $page->begin();
  37.  
  38. $newform = new forms('/class_lib.php', 'GET');
  39. $newlabels = new labels();
  40.  
  41. $newlabels->create_label('Username:');
  42. $newform->create_input('text', 'username');
  43. $newlabels->create_label('Password:');
  44. $newform->create_input('password', 'password');
  45.  
  46. $page->finish();
  47.  
  48. ?>
  49.  
  50. // Create a new form
  51. $newform = new forms('/class_lib.php', 'GET');
  52.  
  53. $newform->create_input('text', 'username');
  54. $newform->create_input('password', 'password');
  55.  
  56. // Output the form, it can use the finish() method because it extends page
  57. $newform->finish();
  58.  
  59. // An interface describes the methods that a class must use
  60. interface renderable
  61. {
  62. // Any classes that implement the renderabe interface must define a method called render()
  63. function render();
  64. }
  65.  
  66. // This abstract class can never be created, so you can never do new element(), it implements renderable
  67. abstract class element implements renderable
  68. {
  69. // Set up some variables for all elemnts
  70. var $attribs = array();
  71. var $name = "";
  72. var $type = "";
  73.  
  74. // The construct for a element needs a type and a name
  75. function __construct($type, $name)
  76. {
  77. $this->name = $name;
  78. $this->type = $type;
  79. }
  80.  
  81. // Set an attribute for the element
  82. function setAttribute($name, $value)
  83. {
  84. $this->attribs[$name] = $value;
  85. }
  86.  
  87. // Get the name of this element
  88. function getName()
  89. {
  90. return $this->name;
  91. }
  92.  
  93. // The render function outputs an element
  94. function render()
  95. {
  96. // Output the start of the element eg <input
  97. echo "<" . $this->type . " ";
  98.  
  99. // each attribute eg class='blue'
  100. foreach($this->attribs as $name => $value)
  101. echo " " . $name . "='" . $value ."' ";
  102.  
  103. // end the element
  104. echo " />";
  105.  
  106. echo "<br />";
  107. }
  108. }
  109.  
  110. // The input element extends element but is not abstract
  111. class input extends element
  112. {
  113. // Nothing is overridden here from the parent class element
  114. }
  115.  
  116. // The label element extends element but is not abstract
  117. class label extends element
  118. {
  119. // Define a new var called label, this is special for the label element
  120. var $label = "";
  121.  
  122. // Override the contruct for element to only accept a name, this
  123. // is because the label element type will always be label
  124. function __construct($name)
  125. {
  126. $this->name = $name;
  127. $this->type = "label";
  128. }
  129.  
  130. // Set the label var
  131. function setLabel($label)
  132. {
  133. $this->label = $label;
  134. }
  135.  
  136. // Override the render function, this means that label has its own render function
  137. // and does not use the function from the abstract class element
  138. function render()
  139. {
  140. echo "<" . $this->type . " ";
  141.  
  142. foreach($this->attribs as $name => $value)
  143. echo " " . $name . "='" . $value ."' ";
  144.  
  145. echo " >";
  146.  
  147. // Here the special label content is displayed
  148. echo $this->label;
  149.  
  150. echo "</label>";
  151. }
  152. }
  153.  
  154. // A form extends element
  155. class form extends element
  156. {
  157. // A form has some new vars
  158. var $elements = array();
  159. var $labels = array();
  160.  
  161. var $action;
  162. var $method;
  163.  
  164. // Override the contruct and use name, action and method
  165. // There are default values for action and method so they are not required
  166. function __construct($name, $action = "/", $method = "GET")
  167. {
  168. $this->name = $name;
  169. $this->type = "form";
  170. $this->action = $action;
  171. $this->method = $method;
  172. }
  173.  
  174. // Add a new element to the form along with its label
  175. function appendElement($element, $label)
  176. {
  177. // Add these to an array inside this class
  178. $this->elements[$element->getName()] = $element;
  179. $this->labels[$label->getName()] = $label;
  180. }
  181.  
  182. // Override the render function
  183. function render()
  184. {
  185. // Output the form's start along with the method and action
  186. echo '<' . $this->type. ' ' . 'action="' . $this->action . '" method="' . $this->method . '" />';
  187.  
  188. // Iterate over the array of elments and render each one
  189. foreach($this->elements as $name => $ele)
  190. {
  191. // Render the label for the current element
  192. $this->labels[$name]->render();
  193. // Render the element
  194. $ele->render();
  195. }
  196.  
  197. // End the form
  198. echo "</form>";
  199. }
  200. }
  201.  
  202. // Create form with name, action and method
  203. $form = new form("login", "/login.php", "POST");
  204.  
  205.  
  206. // Create input username
  207. $ele = new input("input", "username");
  208. // Set type
  209. $ele->setAttribute("type", "text");
  210. // Set a class
  211. $ele->setAttribute("class", "blue");
  212.  
  213. // Create a label for the username long with its content
  214. $label = new label("username");
  215. $label->setLabel("Username: ");
  216.  
  217. // Add the username element and its label
  218. $form->appendElement($ele, $label);
  219.  
  220. // Repeat for password
  221. $ele = new input("input", "password");
  222. $ele->setAttribute("type", "password");
  223.  
  224. $label = new label("password");
  225. $label->setLabel("Password: ");
  226.  
  227. $form->appendElement($ele, $label);
  228.  
  229. // Render the form
  230. $form->render();
  231.  
  232. abstract class HTMLWriter
  233. {
  234. protected $html = '';
  235. protected $tagName = null;
  236. protected $selfClosing = false;
  237. protected $elements = array();
  238. protected $attributes = array();
  239.  
  240. protected $closed = false;
  241.  
  242. abstract public function __construct();
  243.  
  244. public function addElement(HTMLWriter $element)
  245. {
  246. if ($this->closed || $this->selfClosing) {
  247. return;
  248. }
  249. $element->close(); // automatic!
  250.  
  251. $this->elements[] = $element->write();
  252. }
  253.  
  254. public function addElements() {
  255. foreach (func_get_args() as $arg) {
  256. if ($arg instanceof HTMLWriter) {
  257. $this->addElement($arg);
  258. }
  259. }
  260. }
  261.  
  262. public function addAttribute($name, $value)
  263. {
  264. return $this->attributes[$name] = $value;
  265. }
  266.  
  267. public function write()
  268. {
  269. if (!$this->closed) {
  270. $this->close();
  271. }
  272. return $this->html;
  273. }
  274.  
  275. public function close()
  276. {
  277. $this->closed = true;
  278. $this->html = '<' . $this->tagName;
  279. foreach ($this->attributes AS $attr => $val) {
  280. $this->html .= ' ' . $attr . '="' . $val . '"';
  281. }
  282. if ($this->selfClosing) {
  283. $this->html .= '/>';
  284. return;
  285. }
  286. $this->html .= '>';
  287. foreach($this->elements as $elem) {
  288. $this->html .= $elem;
  289. }
  290. $this->html .= '</' . $this->tagName . '>';
  291. }
  292. }
  293.  
  294. abstract class HTMLWriterWithTextNodes extends HTMLWriter
  295. {
  296. //abstract public function __construct();
  297.  
  298. public function addText($text)
  299. {
  300. $this->elements[] = htmlentities($text);
  301. }
  302.  
  303. public function addTextRaw($text)
  304. {
  305. $this->elements[] = $text;
  306. }
  307. }
  308.  
  309. class Form extends HTMLWriter
  310. {
  311. public function __construct($action, $method, $can_upload = false)
  312. {
  313. $this->tagName = 'form';
  314. $this->addAttribute('action', $action);
  315. $this->addAttribute('method', $method);
  316. if ($can_upload) {
  317. $this->addAttribte('enctype','multipart/form-data');
  318. }
  319. }
  320. }
  321.  
  322. class Input extends HTMLWriter
  323. {
  324. public function __construct($type, $name, $id = null)
  325. {
  326. $this->tagName = 'input';
  327. $this->selfClosing = true;
  328. $this->addAttribute('type', $type);
  329. $this->addAttribute('name', $name);
  330. if (!is_null($id)) {
  331. $this->addAttribute('id', $id);
  332. }
  333. }
  334.  
  335. // overrides
  336. public function addElement()
  337. {
  338. return false;
  339. }
  340. }
  341.  
  342. class Label extends HTMLWriterWithTextNodes
  343. {
  344. public function __construct($labelText = null, $for = null)
  345. {
  346. $this->tagName = 'label';
  347.  
  348. if (!is_null($labelText)) {
  349. $this->elements[] = $labelText;
  350. }
  351.  
  352. if (!is_null($for)) {
  353. $this->addAttribute('for', $for);
  354. }
  355. }
  356. }
  357.  
  358. class GenericElement extends HTMLWriterWithTextNodes
  359. {
  360. public function __construct($tagName, $selfClosing = false)
  361. {
  362. if (empty($tagName)) {
  363. $this->closed = true;
  364. $this->html = '';
  365. return;
  366. }
  367. $this->tagName = $tagName;
  368. $this->selfClosing = (bool)$selfClosing;
  369. }
  370. }
  371.  
  372. $form = new Form('/class_lib.php','get');
  373.  
  374. $username = new Input('text','username','username');
  375. $password = new Input('password','password','password');
  376. $submit = new Input('submit','login');
  377. $submit->addAttribute('value','login');
  378. $ulabel = new Label('Username: ', 'username');
  379. $plabel = new Label('Password: ','password');
  380. $br = new GenericElement('br',true);
  381.  
  382. $form->addElements(
  383. $ulabel, $username, $br,
  384. $plabel, $password, $br,
  385. $submit
  386. );
  387.  
  388. echo $form->write();
  389.  
  390. <form action="/class_lib.php" method="get"><label for="username">Username: </label><input type="text" name="username" id="username"/><br/><label for="password">Password: </label><input type="password" name="password" id="password"/><br/><input type="submit" name="login" value="login"/></form>
Add Comment
Please, Sign In to add comment