karlakmkj

php class & object

Sep 21st, 2021
538
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. class Beverage {
  3.   public $temperature, $color, $opacity;
  4.    
  5.   function getInfo(){
  6.     return "This beverage is $this->temperature and $this->color.";
  7.   }
  8. }
  9.  
  10. $tea = new Beverage(); //to create new object
  11. $tea -> temperature = "hot"; //don't need $ for property
  12. echo $tea-> temperature;
  13.  
  14. $soda = new Beverage();
  15. $soda->color = "black";
  16. $soda->temperature = "cold";
  17. echo $soda->getInfo(); // to invoke method
RAW Paste Data