Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.18 KB | None | 0 0
  1. <?php
  2.  
  3. class People {
  4.   public $name;
  5.  
  6.   public function setName($x) {
  7.     $this->name = $x;
  8.   }
  9. }
  10.  
  11. class Tuna {
  12.   public $fish1 = "stinky";
  13.   private $fish2 = "smellgoody";
  14.   public function sayName() {
  15.     echo $this->fish2;
  16.   }
  17. }
  18.  
  19. class Beef {
  20.   private $cowsName;
  21.  
  22.   //setter (sets private variable)
  23.   public function setCowsName($x) {
  24.     $this->cowsName = $x;
  25.   }
  26.  
  27.   //getter (retrieves private variables)
  28.   public function getCowsName(){
  29.     return $this->cowsName;
  30.   }
  31. }
  32.  
  33.  
  34. ?>
  35.  
  36. <html>
  37.   <head>
  38.     <title>PHP Objects</title>
  39.   </head>
  40.   <body>
  41.     <?php
  42.       $person1 = new People();
  43.       $person1->setName('Chris');
  44.       echo $person1->name;
  45.      
  46.       echo "<br />";
  47.      
  48.       $fish = new Tuna();
  49.       echo $fish->fish1;
  50.       $fish->sayName();
  51.      
  52.       echo "<br />";
  53.      
  54.       $cowObject = new Beef;
  55.      
  56.       echo $cowObject->setCowsName('Chris');
  57.       echo $cowObject->getCowsName();
  58.       echo $cowObject->setCowsName(' & Missy');
  59.       echo $cowObject->getCowsName();
  60.      
  61.     ?>
  62.   </body>
  63. </html>
  64.  
  65. <!--
  66.  
  67. Class
  68. Holder for data
  69. variable == field
  70.  
  71. Object
  72. Way to access info in classes
  73.  
  74. -->
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement