Advertisement
Yousuf1791

oop class and object

Jan 4th, 2021
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.58 KB | None | 0 0
  1. <!-- ---------1--------- -->
  2. <?php
  3. class Car {
  4.  
  5.   public $name;
  6.  
  7.   function set_name($n) {
  8.     $this->name = $n;
  9.   }
  10.   function get_name() {
  11.     return $this->name;
  12.   }
  13. }
  14.  
  15. $obj = new Car();
  16. $obj->set_name("BMW");
  17. echo $obj->get_name().", ";
  18.  
  19. $obj->set_name("TOYOTA");
  20. echo $obj->get_name().", ";
  21.  
  22. $obj->set_name("Lamborgini");
  23. echo $obj->get_name().", ";
  24.  
  25. ?>
  26.  
  27. <!-- ---------2--------- -->
  28.  
  29. <?php
  30. class Car {
  31.  
  32.   public $color;
  33.  
  34.   function set_color($color) {
  35.     $this->color = $color;
  36.   }
  37.   function get_color() {
  38.     return $this->color;
  39.   }
  40. }
  41.  
  42. $obj = new Car();
  43.  
  44. $obj->set_color('Red');
  45.  
  46. echo "Color: " . $obj->get_color();
  47.  
  48. $obj->set_color('Green');
  49.  
  50. echo "<br>Color: " . $obj->get_color();
  51.  
  52. $obj->set_color('Blue');
  53.  
  54. echo "<br>Color: " . $obj->get_color();
  55. ?>
  56.  
  57. <!-- ---------3--------- -->
  58. <?php
  59. class Student {
  60.  
  61.   public $name;
  62.  
  63.   function std_name($n) {
  64.     $this->name = $n;
  65.     return $this->name;
  66.   }
  67. }
  68.  
  69. $obj = new Student();
  70. echo $obj->std_name("Yousuf");
  71. ?>
  72.  
  73.  
  74. <!-- ---------4--------- -->
  75. <?php
  76. class country {
  77.   public $name;
  78.   public $color;
  79.  
  80.   function __construct($name) {
  81.     $this->name = $name;
  82.   }
  83.   function name() {
  84.     return $this->name;
  85.   }
  86. }
  87.  
  88. $c_name = new country("Bangladesh");
  89. echo $c_name->name();
  90. ?>
  91.  
  92. <!-- ---------5--------- -->
  93. <?php
  94. class country {
  95.   public $name;
  96.   public $color;
  97.  
  98.   function __construct($name="No name") {
  99.     $this->name = $name;
  100.   }
  101.   function name() {
  102.     return $this->name;
  103.   }
  104. }
  105.  
  106. $c_name = new country("Bangladesh");
  107. echo $c_name->name();
  108. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement