Introduction to OOP in PHP
Learn the basics of object-oriented programming in PHP.
Introduction to OOP in PHP is a free PHP Academy lesson on CoddyKit — lesson 1 of 5. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 5 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Introduction to OOP in PHP
Welcome to the Object-Oriented Programming (OOP) category! In this lesson, you’ll learn the fundamental concepts of OOP and how PHP supports this programming paradigm. Let’s get started!

2
What Is Object-Oriented Programming?
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of objects. Objects are instances of classes that contain properties and methods.
Key Concepts:
- Properties: Variables that hold data specific to an object.
- Methods: Functions that define the behavior of an object.
Tip: OOP makes your code more modular, reusable, and easier to maintain.
3
Why Use OOP?
OOP offers several advantages:
- Modularity: Code is organized into classes, making it easier to understand and maintain.
- Reusability: Classes and objects can be reused across projects.
- Encapsulation: Data is hidden inside objects, protecting it from accidental modifications.
- Inheritance: Classes can inherit properties and methods from other classes.
- Polymorphism: Objects can take on multiple forms, enhancing flexibility.
Key Point: OOP is ideal for building complex and scalable applications.
4
Classes and Objects
A class is a blueprint for creating objects. An object is an instance of a class with its own data and behavior. Example:
Tip: Use the new keyword to create objects from a class.
<?php
class Car {
public $make;
public $model;
public function drive() {
echo "The car is driving!";
}
}
// Creating an object
$myCar = new Car();
$myCar->make = "Toyota";
$myCar->model = "Corolla";
echo "Make: " . $myCar->make . ", Model: " . $myCar->model;
$myCar->drive();
?>
5
Properties in Classes
Properties are variables inside a class. They define the data that an object can hold. Example:
Key Point: Use public, protected, or private to control property access.
<?php
class Person {
public $name;
public $age;
}
$person = new Person();
$person->name = "Alice";
$person->age = 25;
echo "Name: " . $person->name . ", Age: " . $person->age;
?>
6
Methods in Classes
Methods are functions defined inside a class. They describe the behavior of objects. Example:
Tip: Use methods to define actions that objects can perform.
<?php
class Animal {
public function speak() {
echo "The animal makes a sound.";
}
}
$animal = new Animal();
$animal->speak();
?>
7
Constructors
A constructor is a special method that is automatically called when an object is created. Use it to initialize properties. Example:
Key Point: Use __construct() to define a constructor.
<?php
class Book {
public $title;
public $author;
public function __construct($title, $author) {
$this->title = $title;
$this->author = $author;
}
public function getDetails() {
return "Title: $this->title, Author: $this->author";
}
}
$book = new Book("1984", "George Orwell");
echo $book->getDetails();
?>
8
Encapsulation
Encapsulation restricts direct access to an object’s data. Use private properties and provide getter and setter methods. Example:
Tip: Use private for properties that should not be accessed directly.
<?php
class BankAccount {
private $balance;
public function __construct($initialBalance) {
$this->balance = $initialBalance;
}
public function getBalance() {
return $this->balance;
}
public function deposit($amount) {
$this->balance += $amount;
}
}
$account = new BankAccount(100);
$account->deposit(50);
echo "Balance: " . $account->getBalance();
?>
9
10
Great Job!
Congratulations! You’ve learned the basics of Object-Oriented Programming (OOP) in PHP, including classes, objects, properties, methods, and constructors. In the next lesson, we’ll dive deeper into inheritance and explore how classes can share functionality. Let’s keep coding!

Frequently asked questions
Is the “Introduction to OOP in PHP” lesson free?
Yes — the full text of “Introduction to OOP in PHP” is free to read here on the web, and the PHP Academy course includes 5 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Introduction to OOP in PHP”?
Learn the basics of object-oriented programming in PHP. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 5, so you can start here or from the beginning and move at your own pace.
How long does the “Introduction to OOP in PHP” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Introduction to OOP in PHP
- Defining Classes and Objects
- Inheritance
- Encapsulation and Access Modifiers
- Polymorphism