0Pricing
PHP Academy · Lesson

Encapsulation and Access Modifiers

Protect and control access to properties and methods.

Encapsulation and Access Modifiers is a free PHP Academy lesson on CoddyKit — lesson 4 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

Encapsulation and Access Modifiers

Welcome to the lesson on encapsulation! In this lesson, you’ll learn how encapsulation helps protect the data inside classes and how access modifiers control the visibility of class properties and methods. Let’s dive in!

Encapsulation and Access Modifiers — illustration 1

2

What Is Encapsulation?

Encapsulation is the practice of bundling data (properties) and methods that operate on that data within a class and restricting direct access to them from outside the class.

Key Features of Encapsulation:

  • Protects the internal state of an object.
  • Provides controlled access to class properties using methods.
  • Helps maintain data integrity.

Tip: Encapsulation is a fundamental concept for building secure and modular applications.

3

Access Modifiers in PHP

Access modifiers define the visibility of class properties and methods. PHP provides three levels of visibility:

  • public: Accessible from anywhere.
  • protected: Accessible only within the class and its subclasses.
  • private: Accessible only within the class itself.

Key Point: Use access modifiers to control how properties and methods are accessed and modified.

4

Using public Properties

Properties declared as public can be accessed and modified from anywhere. Example:

Tip: Use public for properties and methods meant to be openly accessed.

<?php
  class Car {
    public $make;
    public $model;

    public function drive() {
      echo "The car is driving.";
    }
  }

  $car = new Car();
  $car->make = "Toyota";
  $car->model = "Corolla";

  echo "Make: " . $car->make . ", Model: " . $car->model;
  $car->drive();
?>

5

Using private Properties

Properties declared as private can only be accessed within the class. Example:

Key Point: Use private for properties that need to be protected from external modification.

<?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(); // Outputs: Balance: 150
?>

6

Using protected Properties

Properties declared as protected are accessible within the class and its child classes. Example:

Key Point: Use protected to allow child classes to access parent properties and methods.

<?php
  class Vehicle {
    protected $fuel;

    public function setFuel($fuel) {
      $this->fuel = $fuel;
    }
  }

  class Car extends Vehicle {
    public function getFuel() {
      return $this->fuel;
    }
  }

  $car = new Car();
  $car->setFuel("Gasoline");
  echo "Fuel: " . $car->getFuel(); // Outputs: Fuel: Gasoline
?>

7

Getters and Setters

Encapsulation often involves using getter and setter methods to provide controlled access to private properties. Example:

Tip: Use setters to validate data before modifying properties.

<?php
  class User {
    private $password;

    public function setPassword($password) {
      $this->password = $password;
    }

    public function getPassword() {
      return $this->password;
    }
  }

  $user = new User();
  $user->setPassword("my_secure_password");
  echo "Password: " . $user->getPassword();
?>

8

Benefits of Encapsulation

Encapsulation provides the following advantages:

  • Protects the internal state of an object.
  • Improves code maintainability by centralizing control over properties.
  • Prevents accidental modification of data.
  • Enhances security by restricting direct access to sensitive data.

Key Point: Encapsulation is a fundamental principle of secure and modular OOP.

9

10

Great Job!

Congratulations! You’ve learned about encapsulation and how access modifiers control the visibility of class properties and methods. In the next lesson, we’ll explore polymorphism and how it allows flexibility in object behavior. Let’s keep coding!

Encapsulation and Access Modifiers — illustration 10

Frequently asked questions

Is the “Encapsulation and Access Modifiers” lesson free?

Yes — the full text of “Encapsulation and Access Modifiers” 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 “Encapsulation and Access Modifiers”?

Protect and control access to properties and methods. 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 4 of 5, so you can start here or from the beginning and move at your own pace.

How long does the “Encapsulation and Access Modifiers” 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

  1. Introduction to OOP in PHP
  2. Defining Classes and Objects
  3. Inheritance
  4. Encapsulation and Access Modifiers
  5. Polymorphism
← Back to PHP Academy