Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. <?php
  2. class Person {
  3. protected $id;
  4. protected $name;
  5. protected $height;
  6. public static $nofpeople=0;
  7.  
  8. function __construct(){
  9. $this->id=rand(1000,1000000);
  10. print 'ID ' . $this->id . ' is assigned <br>';
  11. Person::$nofpeople++;
  12. }
  13.  
  14. function __destruct(){
  15. print $this->name . ' is being destroyed<br>';
  16. }
  17.  
  18. final function gender(){
  19. return 'n/a';
  20.  
  21. function __get($property){
  22. print 'Accessed to protected property: ' . $property . '<br>';
  23. return $this->$property;
  24. }
  25.  
  26. function __set($property, $value){
  27. switch($property){
  28. case 'name':
  29. $this->name = $value;
  30. break;
  31. case 'height':
  32. $this->height = $value;
  33. break;
  34. default:
  35. print $property . 'not found';
  36. }
  37. print 'Setting ' . $property . ' to ' . $value . '<br>';
  38. }
  39. }
  40. class employee extends person {
  41. function gender(){
  42. return 'female';
  43. }
  44. }
  45.  
  46. $a = new employee();
  47. $a->name = 'Pero';
  48. $a->height = '1,80m';
  49. print $a->name . ' is ' . $a->height . ' tall.<br><br>';
  50.  
  51. $b = new employee();
  52. $b->name = 'Mate';
  53. $b->height = '1,90m';
  54.  
  55. if ($b instanceof Person) {
  56. echo $b->name . ' je osoba: bool(true)<br><br>';
  57.  
  58. print 'Number of people created: ' . Person::$nofpeople . '<br><br>';
  59. }
  60. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement