Module 1.2 Lesson
Module 1.2 Lesson
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:
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.
• 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.
• 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.
A class is a blueprint for creating objects. It defines the properties and methods that the objects created
from the class will have.
class Person {
public $name;
public $age;
$this->name = $name;
$this->age = $age;
• Methods: __construct initializes properties, and greet provides behavior for the object.
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
• Method Call: $person->greet(); invokes the greet method of the Person object.
class Car {
public $model;
$this->model = $model;
echo "A new car model " . $this->model . " has been created.";
• The constructor initializes the $model property and outputs a message when a Car object is
created.
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:
class BankAccount {
private $balance;
$this->balance = $initialBalance;
$this->balance += $amount;
return $this->balance;
$account->deposit(50);
• Public Methods: deposit and getBalance are used to interact with the private property.
Lesson 4: 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.
class Vehicle {
public $make;
$this->make = $make;
• 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 {
Lesson 5: 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 {
return "Woof";
return "Meow";
• 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.
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.
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.
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.
Additional Resources
• Recommended Books: Books on PHP and OOP, such as "PHP Objects, Patterns, and Practice" by
Mika Schwartz.