OOP

(Object-Oriented Programming) A programming paradigm based on objects and classes

Lessons
  • What is OOP?
  • Classes
  • Objects
  • Methods
  • Constructor
  • Inheritance
  • Polymorphism
  • Encapsulation
  • Interfaces
  • Abstraction
What is OOP?

OOP (Object-Oriented Programming) organizes code into objects and classes.

Example:
class Car { public $color = 'red'; }

Example Description:

Defines a Car class with a color property.
Classes

A class is a blueprint for creating objects.

Example:
class Person { public $name; }

Example Description:

Defines a class with a name property.
Objects

Objects are instances created from classes.

Example:
$p = new Person(); $p->name = "Ali";

Example Description:

Creates an object from Person and sets the name
Methods

Methods are functions defined inside a class.

Example:
class User { public function greet() { return "Hello!"; } }

Example Description:

A method greet() returns a greeting message.
Constructor

A constructor is a special method that runs when an object is created.

Example:
class Book { public function __construct() { echo "Book created"; } }

Example Description:

Prints a message when the object is created.
Inheritance

A class can inherit properties and methods from another class

Example:
class Animal { public function move() { echo "Moving..."; } } class Dog extends Animal {}

Example Description:

Dog inherits the move() method from Animal.
Polymorphism

Different classes can use the same method name but implement it differently

Example:
class Shape { public function draw() {} } class Circle extends Shape { public function draw() { echo "Draw Circle"; } }

Example Description:

Each class provides its own version of the draw() method.
Encapsulation

Encapsulation hides internal details and only exposes necessary parts

Example:
class Account { private $balance = 100; public function getBalance() { return $this->balance; } }

Example Description:

You cannot access balance directly; you must use getBalance().
Interfaces

Interfaces define required methods for any implementing class

Example:
interface Logger { public function log($message); }

Example Description:

Any class implementing Logger must define a log() method.
Abstraction

Abstract classes define structure but leave implementation to subclasses.

Example:
abstract class Animal { abstract public function makeSound(); }

Example Description:

Subclasses must define the makeSound() method.
You have completed the lesson . Add To Your Completed Course

You have a test for this course. Do you want to submit it to pass the course?