You are on page 1of 3

3.

Apply Object Oriented Concepts in PHP(16m)


Description:

Creating Classes (Car Class): This code defines a class named Car with properties (attributes) for brand, model, and color. It also
includes a constructor method __construct() to initialize object properties and a method displayDetails() to display car details.

Creating Object ($car1 and $car2): Two objects (instances) of the Car class are created using the new keyword and passed with
specific values to the constructor. The displayDetails() method is then called for each object to display their details.

This example demonstrates the basics of creating classes and objects in PHP, encapsulating data and behavior within a class and
creating multiple instances of that class (objects) to work with.

CONSTRUCTOR AND DESTRUCTOR TAKE FROM ASSIGNEMENT BOOK


### 3.3 Inheritance

- Explanation:
- Inheritance allows a child class to inherit properties and methods from its parent class.
- The child class `ChildClass` inherits the `displayMessage()` method from its parent class `ParentClass`.
- When an object of `ChildClass` is created and the `displayMessage()` method is called, it executes the method from the parent
class.

### 3.3.1 Method of Function Overloading

- Explanation:
- Method overloading is achieved in PHP by defining multiple methods with the same name but different parameters in the same
class or its child classes.
- The child class `ChildClass` overloads the `displayMessage()` method from its parent class `ParentClass` by defining a method
with the same name but different parameters.
- When the overloaded method is called with the appropriate parameters, it executes the overloaded version of the method.

### 3.3.2 Method of Function Overriding

- Explanation:
- Method overriding is achieved in PHP by redefining a method from the parent class in its child class with the same name and
parameters.
- The child class `ChildClass` overrides the `displayMessage()` method from its parent class `ParentClass` by defining a method
with the same name.
- When the overridden method is called, it executes the version of the method defined in the child class, overriding the behavior
of the parent class method.

You might also like