Advertisement
wingman007

OOP_PHP_InheritanceAbstractionEncapsulationPolymorphism

Mar 18th, 2018
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.47 KB | None | 0 0
  1. <?php
  2.  
  3. interface Introducable {
  4.   function introduceYourSelf();
  5. }
  6.  
  7. class Person implements Introducable {
  8.   const PI = 3.14;
  9.   private $name;
  10.   private $family;
  11.   private $age;
  12.   private static $counter = 0;
  13.   // public readonly $size = 0;
  14.  
  15.   function __construct($name, $family, $age) {
  16.     $this->name = $name;
  17.     $this->family = $family;
  18.     $this->age = $age;
  19.     self::$counter++;
  20.   }
  21.  
  22.   function introduceYourSelf() {
  23.     return "My name is $this->name. I am $this->age years old!";
  24.   }
  25.  
  26.   public static function getCounter() {
  27.     return self::$counter;
  28.   }
  29.  
  30.  
  31. }
  32.  
  33. class Student extends Person {
  34.   private $fNumber;
  35.  
  36.   function __construct($name, $family, $age, $fNumber) {
  37.     parent::__construct($name, $family, $age);
  38.     $this->fNumber = $fNumber;
  39.   }
  40.  
  41.   function introduceYourSelf() {
  42.     return parent::introduceYourSelf() . "FNumber = $this->fNumber";
  43.   }
  44.  
  45. }
  46.  
  47. $stoyan = new Person("Stoyan", "Cheresharov", 54);
  48. $kircho = new Student("Kircho", "Ivanov", 24, "312312312");
  49. ?>
  50.  
  51. <h1>Hello world!</h1>
  52. <pre>
  53. <?php
  54.  
  55. echo  $stoyan->introduceYourSelf() . PHP_EOL; // My name is Stoyan. I am 54 years old!
  56.  
  57. echo  Person::getCounter() . PHP_EOL;
  58. echo  $stoyan::getCounter() . PHP_EOL;
  59. echo  $stoyan->getCounter() . PHP_EOL;
  60. echo  Person::PI . PHP_EOL;
  61. echo  $stoyan::PI . PHP_EOL;
  62.  
  63. echo $kircho->introduceYourSelf() . PHP_EOL; // My name is Kircho. I am 24 years old!FNumber = 312312312
  64.  
  65. // phpinfo();
  66. ?>
  67. </pre>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement