0% found this document useful (0 votes)
6 views10 pages

Web 3

A class serves as a blueprint for creating objects, defining properties and methods that characterize those objects. A constructor is a special method in a class that initializes an object's properties upon instantiation. The document provides examples of classes and objects in PHP, including a Car class and a Person class, demonstrating how to create objects and access their properties and methods.

Uploaded by

akkhanyar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views10 pages

Web 3

A class serves as a blueprint for creating objects, defining properties and methods that characterize those objects. A constructor is a special method in a class that initializes an object's properties upon instantiation. The document provides examples of classes and objects in PHP, including a Car class and a Person class, demonstrating how to create objects and access their properties and methods.

Uploaded by

akkhanyar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Object and classes

What is a Class and object?


• a class is a blueprint for creating objects.
• It defines a set of properties and methods
that describe the characteristics and
behaviour of objects created from that class.
• An object is an instance of a class,
• it's a concrete, real-world entity created based on the class definition.
• Class is like a blueprint or template that defines the structure for objects,
• which are concrete instances of that class
Constructor
• In object-oriented programming, a constructor is a special method within
• a class that is automatically executed when an object of that class is instantiated
or created.
• Its primary purpose is to initialize the object's properties or perform any other
setup operations
• that are necessary before the object can be used.

• In PHP, the constructor method is always named __construct().


class MyClass {
// Properties
public $property1;
public $property2;

// Constructor
public function __construct($value1, $value2) {
// Initialize properties
$this->property1 = $value1;
$this->property2 = $value2;

// Additional setup code if needed


echo "Object created with values $value1 and $value2.\n";
}

// Other methods can follow


}

// Creating an object of MyClass triggers the constructor


// Define a class called 'Car'
class Car {
// Properties
public $brand;
public $model;
public $year;

// Constructor
public function __construct($brand, $model, $year) {
$this->brand = $brand;
$this->model = $model;
$this->year = $year;
}
// Method to display information about the car
public function displayInfo() {
echo "Brand: {$this->brand}, Model: {$this->model}, Year: {$this->year}\n";
}
// Creating an object (instance) of the Car class
$myCar = new Car("Toyota", "Camry", 2022);

// Accessing properties and calling methods of the object


$myCar->displayInfo(); // Display information about the car
?>
• class Person {
• // Properties
• public $name;
• public $age;
• public $gender;

• // Constructor
• public function __construct($name, $age, $gender) {
• $this->name = $name;
• $this->age = $age;
• $this->gender = $gender;
• }
public function displayInfo()
{
echo "Name: {$this->name}, Age: {$this->age}, Gender: {$this>gender}\n";
}

$person1 = new Person("John Doe", 25, "Male");

$person1->displayInfo();
• class JConfig {

• private $host = 'localhost';

• //Getter function for $host


• public function getHost() {
• return $this->host;
• {

• //Setter function for $host


• public function setHost($value) {
• $this->host = $value;
• {
• $jc = new JConfig();

• //set it
• $jc->setHost("MyHost");

• //get it
• echo $jc->getHost();

You might also like