Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Module 1.2 Lesson

The document provides an introduction to Object-Oriented Programming (OOP) in PHP, explaining key concepts such as encapsulation, inheritance, polymorphism, and abstraction. It compares OOP with procedural programming, highlights the benefits of OOP including code reusability and maintainability, and includes examples of defining classes and creating objects. Additionally, it outlines the importance of visibility modifiers for encapsulation and demonstrates method overriding in inheritance.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Module 1.2 Lesson

The document provides an introduction to Object-Oriented Programming (OOP) in PHP, explaining key concepts such as encapsulation, inheritance, polymorphism, and abstraction. It compares OOP with procedural programming, highlights the benefits of OOP including code reusability and maintainability, and includes examples of defining classes and creating objects. Additionally, it outlines the importance of visibility modifiers for encapsulation and demonstrates method overriding in inheritance.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Module: Introduction to PHP Object-Oriented Programming (OOP) and Benefits of OOP

Lesson 1.2.1: Introduction to OOP

Object-Oriented Programming (OOP) in PHP is a programming paradigm that uses objects and classes to
structure code, as opposed to procedural programming which relies on functions and logic flowing in a
linear sequence. PHP has supported OOP since version 4, but its capabilities have significantly improved
with each new version. Here's an overview of OOP concepts and the benefits they bring to PHP
development:

1.1 What is Object-Oriented Programming?

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects,"


which are instances of classes. OOP aims to model real-world entities as objects within a program,
making it easier to design and manage complex software systems.

Key Principles:

• Encapsulation: Bundles data (attributes) and methods (functions) that operate on the data into a
single unit called a class. This hides the internal state and requires interaction through public
methods.

• Inheritance: Allows a new class (child) to inherit properties and methods from an existing class
(parent). This promotes code reuse and establishes a hierarchy.

• Polymorphism: Enables objects to be treated as instances of their parent class rather than their
actual class. This allows for a single interface to be used with different underlying forms (data
types).

• Abstraction: Focuses on exposing only the relevant aspects of an object while hiding the
complex implementation details. This simplifies interaction with objects.

1.2 Core Principles of OOP

• Encapsulation: Protects object integrity by preventing outside code from directly accessing or
modifying the internal state of an object. This is achieved through visibility modifiers (public,
private, protected).

• Inheritance: Facilitates creating new classes based on existing ones, which helps in building a
class hierarchy and avoiding code duplication.

• Polymorphism: Allows methods to do different things based on the object’s actual class type,
even when using a common interface. This enhances flexibility and integration.

• Abstraction: Simplifies complex systems by exposing only necessary functionality through


abstract classes or interfaces, making the system easier to manage and use.

1.3 Comparing OOP with Procedural Programming

• Procedural Programming: Focuses on functions and procedures, with data and functions
separated. It's linear and follows a top-down approach.
• Object-Oriented Programming: Encapsulates data and functions into objects. It uses a bottom-
up approach where objects interact with each other. OOP promotes better organization, easier
maintenance, and more reusable code compared to procedural programming.

Lesson 2: Working with Classes and Objects in PHP

2.1 Defining a Class

A class is a blueprint for creating objects. It defines the properties and methods that the objects created
from the class will have.

Example: Basic Person Class

class Person {

public $name;

public $age;

public function __construct($name, $age) {

$this->name = $name;

$this->age = $age;

public function greet() {

return "Hello, my name is " . $this->name;

• Properties: $name and $age are attributes of the class.

• Methods: __construct initializes properties, and greet provides behavior for the object.

2.2 Creating and Using Objects

Objects are instances of a class and represent individual elements of that class. They use the methods
and properties defined in the class.
Example: Creating a Person Object

$person = new Person("Alice", 30);

echo $person->greet(); // Output: Hello, my name is Alice

• Instantiation: $person = new Person("Alice", 30); creates a new Person object.

• Method Call: $person->greet(); invokes the greet method of the Person object.

2.3 Constructor and Destructor Methods

• Constructor (__construct): A special method that is automatically called when an object is


instantiated. It initializes object properties.

• Destructor (__destruct): A special method that is automatically called when an object is


destroyed, often used for cleanup.

Example: Constructor and Destructor

class Car {

public $model;

public function __construct($model) {

$this->model = $model;

echo "A new car model " . $this->model . " has been created.";

public function __destruct() {

echo "The car model " . $this->model . " is being destroyed.";

$car = new Car("Toyota");

• The constructor initializes the $model property and outputs a message when a Car object is
created.

• The destructor outputs a message when the Car object is destroyed.


Lesson 3: Encapsulation

3.1 Understanding Encapsulation

Encapsulation ensures that an object’s data is protected from unauthorized access and modification. It
uses visibility modifiers to control access to the properties and methods of a class.

Visibility Modifiers:

• Public: Accessible from anywhere.

• Private: Accessible only within the class itself.

• Protected: Accessible within the class and its subclasses.

3.2 Example: Encapsulation in PHP

Example: BankAccount Class

class BankAccount {

private $balance;

public function __construct($initialBalance) {

$this->balance = $initialBalance;

public function deposit($amount) {

$this->balance += $amount;

public function getBalance() {

return $this->balance;

$account = new BankAccount(100);

$account->deposit(50);

echo $account->getBalance(); // Output: 150


• Private Property: $balance is not accessible from outside the class.

• Public Methods: deposit and getBalance are used to interact with the private property.

Lesson 4: Inheritance

4.1 Understanding Inheritance

Inheritance allows one class (child) to inherit properties and methods from another class (parent). This
facilitates code reuse and establishes a hierarchical relationship between classes.

Example: Vehicle and Car

class Vehicle {

public $make;

public function __construct($make) {

$this->make = $make;

public function start() {

return "Vehicle started";

class Car extends Vehicle {

public function honk() {

return "Car horn sounds!";

$car = new Car("Honda");

echo $car->start(); // Output: Vehicle started

echo $car->honk(); // Output: Car horn sounds!

• Parent Class: Vehicle provides basic functionality.

• Child Class: Car extends Vehicle and adds new functionality (honk).
4.2 Overriding Methods

Overriding allows a child class to provide a specific implementation of a method that is already defined
in its parent class.

Example: Overriding

class Vehicle {

public function start() {

return "Vehicle started";

class ElectricCar extends Vehicle {

public function start() {

return "Electric car is starting silently.";

$electricCar = new ElectricCar();

echo $electricCar->start(); // Output: Electric car is starting silently.

• The start method in ElectricCar overrides the start method in Vehicle.

Lesson 5: Polymorphism

5.1 Understanding Polymorphism

Polymorphism allows objects of different classes to be treated as objects of a common base class. This is
achieved through method overriding and interfaces.
Example: Polymorphism with Animal Classes

class Animal {

public function makeSound() {

return "Some sound";

class Dog extends Animal {

public function makeSound() {

return "Woof";

class Cat extends Animal {

public function makeSound() {

return "Meow";

$animals = [new Dog(), new Cat()];

foreach ($animals as $animal) {

echo $animal->makeSound() . "<br>";

• Base Class: Animal defines a generic makeSound method.

• Derived Classes: Dog and Cat override the makeSound method to provide specific
implementations.
5.2 Example: Using Polymorphism

Using polymorphism allows different object types to be processed through a common interface or base
class method, making code more flexible and reusable.

Lesson 6: Benefits of OOP

6.1 Code Reusability

OOP encourages code reuse through inheritance and composition, reducing redundancy and promoting
modular design.

Example: A base Shape class can be extended by Circle, Square, etc., each implementing specific
behavior.

6.2 Maintainability

OOP promotes maintainability by organizing code into classes and objects, making it easier to update
and manage complex systems.

Example: Changes in a base class automatically propagate to derived classes, simplifying maintenance.

6.3 Flexibility and Extensibility

OOP allows for the creation of flexible and extensible systems. New classes can be added without
altering existing code.

Example: Adding new vehicle types (e.g., Bike) without changing the existing Car or Vehicle classes.

6.4 Encapsulation and Data Protection

Encapsulation helps protect the internal state of an object from external modification and misuse,
enhancing data integrity.

Example: Using private properties and public methods to control access to sensitive data.

Summary and Review

• Encapsulation: Keeps data safe and secure within objects.

• Inheritance: Promotes code reuse and hierarchical relationships.

• Polymorphism: Enhances flexibility by allowing different object types to be used


interchangeably.

• Abstraction: Simplifies interactions by hiding complex implementation details.

Additional Resources

• PHP Official Documentation on OOP: PHP OOP Documentation

• Recommended Books: Books on PHP and OOP, such as "PHP Objects, Patterns, and Practice" by
Mika Schwartz.

You might also like