Advertisement
karlakmkj

php constructor

Sep 21st, 2021
692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.49 KB | None | 0 0
  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();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement