karlakmkj

php constructor

Sep 21st, 2021
575
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.   //constructor to set parameters to their respective object properties
  10.   function __construct($temperature, $color){
  11.     $this->temperature = $temperature;  
  12.     $this->color = $color;
  13.   }
  14. }
  15. $tea = new Beverage("cold", "black"); // enter arguments to invoke constructor in creating new object
  16. echo $tea->getInfo();
RAW Paste Data