PHP OOP __constructor method

In PHP constructor is a magic methods that is used to initialize the properties of the class.

class Product{
	public $name, $price;

	public function __construct($name, $price){
		$this->name = $name;
		$this->price = $price;
	}
	public function display(){
		echo "
		Name: $this->name <br/>
		Price: $this->price <hr/>
		";
	}
}

$item = new Product("Jeans", 4000);
$item->display();
© 2016 - 2022, All Rights are Reserved.