Advertisement
Guest User

Untitled

a guest
Aug 31st, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1.  
  2. //Overriding Parent Methods
  3.  
  4. //Nice work!
  5.  
  6. //Sometimes we want a child class (or subclass)
  7. to be able to override a property or method of its parent
  8. class (or superclass).
  9.  
  10. <html>
  11. <head>
  12. <title>Override!</title>
  13. </head>
  14. <body>
  15. <p>
  16. <?php
  17. class Vehicle {
  18. public function honk() {
  19. return "HONK HONK!";
  20. }
  21. }
  22. class Bicycle extends Vehicle{
  23. public function honk() {
  24. return "Beep beep!";
  25. }
  26. }
  27. $bicycle = new Bicycle();
  28. echo $bicycle -> honk();
  29. // Add your code below!
  30.  
  31.  
  32. ?>
  33. </p>
  34. </body>
  35. </html>
  36.  
  37.  
  38. //Class Constants and Scope Resolution
  39.  
  40. //We've talked a lot about changing variables in PHP,
  41. but sometimes we want variables that don't change.
  42. These are prefixed with the const keyword (short for constant).
  43.  
  44. //PHP lets us set constants on a class-by-class basis!
  45. Each class has its own scope, which is the context in which
  46. its variables can be used.
  47.  
  48. <html>
  49. <head>
  50. <title>Scope it Out!</title>
  51. </head>
  52. <body>
  53. <p>
  54. <?php
  55. class Person {
  56.  
  57. }
  58. class Ninja extends Person {
  59. // Add your code here...
  60. const stealth = "MAXIMUM";
  61.  
  62. }
  63. echo Ninja::stealth;// ...and here!
  64.  
  65.  
  66. ?>
  67. </p>
  68. </body>
  69. </html>
  70.  
  71.  
  72.  
  73.  
  74.  
  75. The Static Keyword
  76.  
  77. Nice work!
  78.  
  79. You probably noticed that we could access the Ninja class constant
  80. without actually creating an instance of Ninja,
  81. and if you're particularly precocious,
  82. you might be wondering whether it's possible to access class
  83. properties or methods without creating an instance of the class.
  84. The answer: yes!
  85.  
  86. The static keyword lets you use a class' property or
  87. method without having to create an instance of that class.
  88. It works like this:
  89.  
  90. class Person {
  91. public static $isAlive = "Yep!"
  92. public static function greet() {
  93. echo "Hello there!";
  94. }
  95. }
  96.  
  97. echo Person::$isAlive;
  98. // prints "Yep!"
  99. Person::greet();
  100. // prints "Hello there!"
  101.  
  102. When combined with the scope resolution operator, this lets us access class information without having to instantiate anything. Neat, right?
  103.  
  104.  
  105. <html>
  106. <head>
  107. <title></title>
  108. </head>
  109. <body>
  110. <p>
  111. <?php
  112. class King {
  113. // Modify the code on line 10...
  114. public static function proclaim() {
  115. echo "A kingly proclamation!";
  116. }
  117. }
  118. // ...and call the method below!
  119. King::proclaim();
  120.  
  121. ?>
  122. </p>
  123. </body>
  124. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement