0% found this document useful (0 votes)
4 views

WT Experiment8

Web Technology

Uploaded by

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

WT Experiment8

Web Technology

Uploaded by

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

Experiment 8

Title : Experiment Based on basic OOP concepts in PHP

Aim : To make student understand the basic OOP concepts using PHP
and usage of the same

Basic OOP concepts


Following are the basic Object Oriented Concepts in PHP:-
 Class − A class is a template for objects. When the individual objects
are created, they inherit all the properties and behaviours from the
class, but each object will have different values for the properties.

 Object - Object is an instance of class. We can create multiple objects


from a class.

 Member Variable − These are the variables defined inside a class. This
data will be invisible to the outside of the class and can be accessed via
member functions. These variables are called attribute of the object once an
object is created.

 Member function − These are the function defined inside a class and are
used to access object data.
 Abstraction - Abstraction in object-oriented programming (OOP)
refers to the concept of hiding the complex implementation details of
an object and exposing only the essential features or functionalities
 Encapsulation – In encapsulation the internal details are hidden
from external code, and access is controlled through these public
methods, providing a level of abstraction
 Inheretance - Inheritance is a concept where a new class (called the
child or subclass) can inherit attributes and behaviors (properties and
methods) from an existing class (called the parent or superclass). This
allows you to create a relationship between classes.
 Constructor – It is a special type of member function which will be called
automatically whenever there is an object formation from a class.
 Destructor − It is a special type of member function which will be called
automatically whenever an object is deleted or goes out of scope.

Practical implementation of OOPs concepts learned so far


class Car {
// properties (attributes)
$model;
$color;
$fuelLevel;

public function __construct($model, $color) {


$this->model = $model;
$this->color = $color;
$this->fuelLevel = 100;
}

// Public method to get the model of the car


public function getModel() {
return $this->model;
}

// Public method to get the color of the car


public function getColor() {
return $this->color;
}

// Public method to get the fuel level of the car


public function getFuelLevel() {
return $this->fuelLevel;
}

// Public method to simulate driving, reducing fuel level


public function drive() {
// Simulate driving by reducing fuel level
$this->fuelLevel -= 10;
if ($this->fuelLevel < 0) {
$this->fuelLevel = 0; // Ensure fuel level doesn't go below 0
}
}
}

$myCar = new Car("Toyota", "Blue");


echo "Model: " . $myCar->getModel() . "<br>";
echo "Color: " . $myCar->getColor() . "<br>";
echo "Fuel Level: " . $myCar->getFuelLevel() . "%<br>";

$myCar->drive();

echo "Updated Fuel Level after driving: " . $myCar->getFuelLevel() . "%";

Conclusion:-

Thus, we have studied, understood and practically checked OOP concepts in php.

You might also like