PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/PHP/Tutorials/30 days of code/Day13AbstractClasses.php

https://gitlab.com/vladwalross/datasnok
PHP | 91 lines | 38 code | 7 blank | 46 comment | 0 complexity | d239c31ef13bf053e7db341e40d3a7f4 MD5 | raw file
  1. <?php
  2. /*
  3. Objective
  4. Today, we're taking what we learned yesterday about Inheritance and extending it to Abstract Classes. Because this is a very specific Object-Oriented concept, submissions are limited to the few languages that use this construct. Check out the Tutorial tab for learning materials and an instructional video!
  5. Task
  6. Given a Book class and a Solution class, write a MyBook class that does the following:
  7. Inherits from Book
  8. Has a parameterized constructor taking these parameters:
  9. string
  10. string
  11. int
  12. Implements the Book class' abstract display() method so it prints these lines:
  13. , a space, and then the current instance's .
  14. , a space, and then the current instance's .
  15. , a space, and then the current instance's .
  16. Note: Because these classes are being written in the same file, you must not use an access modifier (e.g.: ) when declaring MyBook or your code will not execute.
  17. Input Format
  18. You are not responsible for reading any input from stdin. The Solution class creates a Book object and calls the MyBook class constructor (passing it the necessary arguments). It then calls the display method on the Book object.
  19. Output Format
  20. The method should print and label the respective , , and of the MyBook object's instance (with each value on its own line) like so:
  21. Title: $title
  22. Author: $author
  23. Price: $price
  24. Note: The is prepended to variable names to indicate they are placeholders for variables.
  25. Sample Input
  26. The following input from stdin is handled by the locked stub code in your editor:
  27. The Alchemist
  28. Paulo Coelho
  29. 248
  30. Sample Output
  31. The following output is printed by your display() method:
  32. Title: The Alchemist
  33. Author: Paulo Coelho
  34. Price: 248
  35. */
  36. class Day13AbstractClasses {
  37. function __construct()
  38. {
  39. $title = fgets(STDIN);
  40. $author = fgets(STDIN);
  41. $price = intval(fgets(STDIN));
  42. $novel = new MyBook($title, $author, $price);
  43. $novel->display();
  44. }
  45. }
  46. class MyBook extends Book {
  47. function __construct($t, $a, $p)
  48. {
  49. $this->title = $t;
  50. $this->author = $a;
  51. $this->price = $p;
  52. }
  53. function display()
  54. {
  55. fwrite(STDOUT, "Title: {$this->title}");
  56. fwrite(STDOUT, "Author: {$this->author}");
  57. fwrite(STDOUT, "Price: {$this->price}");
  58. }
  59. }
  60. abstract class Book
  61. {
  62. protected $title;
  63. protected $author;
  64. function __construct($t, $a)
  65. {
  66. $this->title = $t;
  67. $this->author = $a;
  68. }
  69. abstract protected function display();
  70. }
  71. new Day13AbstractClasses();
  72. ?>