WBP Ut2
WBP Ut2
Q1) ANY 4
1. State the Role of Constructor.
Ans: A constructor is a special method in object-oriented programming that initializes an
object when it is created.
It automatically executes when an instance of a class is created.
Q2) ANY 3
1. Explain class and object with a suitable example in PHP.
Ans:
i. Class:
• A class is a template that defines attributes (variables) and behaviors (methods).
• It is created using the class keyword in PHP.
• Syntax: class class_name{
// attribute and behaviors
}
ii. Object:
• An object is an instance of a class.
• It is created using the new keyword.
• Syntax: $object_name = new class_name();
Example:
<html>
<body>
<?php
// Defining a class
class Car {
public $brand;
public $color;
// Constructor
public function __construct($brand, $color) {
$this->brand = $brand;
$this->color = $color;
}
public function displayInfo() {
echo "This car is a " . $this->color . " " . $this->brand . ".";
}
}
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
O/P=>
Record deleted successfully.