Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.87 KB | None | 0 0
  1. <?php
  2.  
  3. class Person {
  4.  
  5.   protected $name;
  6.  
  7.   public function __construct($name) {
  8.     $this->name = $name;
  9.   }
  10.  
  11.   public function getIntroduction() {
  12.     return sprintf(
  13.       'My name is %s',
  14.       $this->name
  15.     );
  16.   }
  17. }
  18.  
  19. class WorkerPerson extends Person {
  20.  
  21.   protected $occupation;
  22.  
  23.   public function __construct($name, $occupation) {
  24.  
  25.     parent::__construct($name);
  26.     $this->occupation = $occupation;
  27.   }
  28.  
  29.   public function getIntroduction() {
  30.     return sprintf(
  31.       '%s and my occupation is %s',
  32.       parent::getIntroduction(),
  33.       $this->occupation
  34.     );
  35.   }
  36. }
  37.  
  38. $person = new Person('Joaquin');
  39. echo $person->getIntroduction();
  40.  
  41.  
  42. // This prints "My name is Joaquin"
  43.  
  44. $worker = new WorkerPerson('Joaquin', 'web development');
  45. echo $worker->getIntroduction();
  46. // This prints "My name is Joaquin and my occupation is web development"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement