0Pricing
PHP Academy · Lesson

Defining Classes and Objects

Create and use classes and objects in PHP.

Defining Classes and Objects is a free PHP Academy lesson on CoddyKit — lesson 2 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

Defining Classes and Objects

Welcome to the next lesson! In this lesson, you’ll learn how to define classes and create objects in PHP. Let’s build the foundation for object-oriented programming in your applications!

Defining Classes and Objects — illustration 1

2

What Are Classes?

A class is a blueprint for creating objects. It defines properties (data) and methods (behavior) that the objects of the class will have.

Example: A Car class might define properties like make and model, and methods like drive() and stop().

3

Defining a Class

To define a class, use the class keyword followed by the class name. Example:

Key Point: Class names should start with a capital letter and follow camel case (e.g., MyClass).

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

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

4

What Are Objects?

An object is an instance of a class. When you create an object, you are creating a specific instance of a class with its own data and behavior.

Example: A Car object might represent a Toyota Corolla or a Honda Civic.

Key Point: Use the new keyword to create objects from a class.

5

Creating Objects

To create an object, use the new keyword with the class name. Example:

Tip: Use the arrow operator (->) to access properties and methods of an object.

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

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

6

Adding Methods to a Class

Methods define the behavior of a class. Use the function keyword inside the class to add methods. Example:

Key Point: Methods allow objects to perform actions or operations.

<?php
  class Animal {
    public $name;

    public function makeSound() {
      echo "The animal makes a sound.";
    }
  }

  $dog = new Animal();
  $dog->name = "Dog";
  $dog->makeSound();
?>

7

Using Constructors

Constructors are special methods automatically called when an object is created. Use constructors to initialize properties. Example:

<?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

Property Visibility

Properties can have different visibility levels:

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

Example:

Key Point: Use visibility to protect sensitive data.

<?php
  class User {
    private $password;

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

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

  $user = new User();
  $user->setPassword("secret");
  echo $user->getPassword();
?>

9

10

Great Job!

Congratulations! You’ve learned how to define classes and create objects in PHP, including adding properties, methods, and constructors. In the next lesson, we’ll explore inheritance and how to extend functionality across classes. Let’s keep coding!

Defining Classes and Objects — illustration 10

Frequently asked questions

Is the “Defining Classes and Objects” lesson free?

Yes — the full text of “Defining Classes and Objects” 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 “Defining Classes and Objects”?

Create and use classes and objects 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 2 of 5, so you can start here or from the beginning and move at your own pace.

How long does the “Defining Classes and Objects” 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