What is Constructor in PHP

<?php 
class Car
{
// Properties
public $name;
public $color;
function __construct($name,$color)
{
$this->name = $name;
$this->color = $color;
}
}
?>
<?php 
class Car
{
// Properties
public $name;
public $color;
// Methods
function set_name($name)
{
$this->name = $name;
}
function set_color($color)
{
$this->color = $color;
}
}
?>
<!DOCTYPE html> 
<html>
<body>
<?php
class Car
{
// Properties
public $name;
public $color;
function __construct($name,$color)
{
$this->name = $name;
$this->color = $color;
}
// Methods
function get_name()
{
return $this->name;
}
function get_color()
{
return $this->color;
}
}
//create a new object
$first_car = new Car("Maruti","Red");
echo "The name of the car is: ".$first_car->get_name();
echo "<br>";
echo "The color of the car is: ".$first_car->get_color(); ?>
</body>
</html>

--

--

Hi, This is Biplab and I am web developer well versed in PHP and other open source technologies. I am here to share my experience with the community.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Bipsmedium

Bipsmedium

25 Followers

Hi, This is Biplab and I am web developer well versed in PHP and other open source technologies. I am here to share my experience with the community.