0Pricing
PHP Academy · Lesson

Classes, Properties, and Methods

Define a PHP class with properties and methods.

Classes, Properties, and Methods is a free PHP Academy lesson on CoddyKit — lesson 1 of 4. 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 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Introduction: PHP Classes

A class is a blueprint for creating objects. It defines properties (data) and methods (behavior). PHP's OOP features make code more modular, reusable, and maintainable.

Core Concepts

PHP Classes key concepts:

  • Use class ClassName {} to define
  • Properties store data, methods define behavior
  • Create instances with new ClassName()
  • Access members with -> arrow

Defining a Class

Example:

<?php
class Car {
    public string $brand;
    public int    $year;
    public float  $price;

    public function describe(): string {
        return "{$this->year} {$this->brand}";
    }
}

$car = new Car();
$car->brand = 'Toyota';
$car->year  = 2022;
$car->price = 25000.0;

echo $car->describe(); // 2022 Toyota

Access Modifiers

Continued:

<?php
class BankAccount {
    public  string $owner;
    private float  $balance;
    protected string $currency;

    // public: accessible everywhere
    // private: only within this class
    // protected: this class + subclasses
}

Properties with Types

Practice:

<?php
class Product {
    public string  $name;
    public float   $price;
    public int     $stock = 0;  // default value
    public bool    $active = true;

    public function inStock(): bool {
        return $this->stock > 0;
    }
}

Instance Methods

Deeper:

<?php
class Counter {
    private int $count = 0;

    public function increment(): void { $this->count++; }
    public function decrement(): void { $this->count--; }
    public function reset(): void    { $this->count = 0; }
    public function value(): int      { return $this->count; }
}

$c = new Counter();
$c->increment();
$c->increment();
echo $c->value();  // 2

Method Chaining

Advanced usage:

<?php
class QueryBuilder {
    private string $table = '';
    private int    $limit = 100;

    public function from(string $t): static { $this->table = $t; return $this; }
    public function limit(int $n): static   { $this->limit = $n; return $this; }
    public function build(): string {
        return "SELECT * FROM {$this->table} LIMIT {$this->limit}";
    }
}

echo (new QueryBuilder())->from('users')->limit(10)->build();

Class Constants

Real-world pattern:

<?php
class HttpStatus {
    const OK        = 200;
    const NOT_FOUND = 404;
    const ERROR     = 500;

    public static function message(int $code): string {
        return match($code) {
            self::OK        => 'OK',
            self::NOT_FOUND => 'Not Found',
            default         => 'Error',
        };
    }
}

echo HttpStatus::message(HttpStatus::NOT_FOUND);  // Not Found

Readonly Properties (PHP 8.1)

Best practices:

<?php
class Point {
    public function __construct(
        public readonly float $x,
        public readonly float $y,
    ) {}

    public function distanceTo(Point $other): float {
        return sqrt(($this->x - $other->x) ** 2 + ($this->y - $other->y) ** 2);
    }
}

$p1 = new Point(0, 0);
$p2 = new Point(3, 4);
echo $p1->distanceTo($p2);  // 5

Practical Pattern

Putting it all together with a practical pattern for PHP Classes.

Common Pitfalls

Avoid these common mistakes with PHP Classes:

  • Use access modifiers to encapsulate state
  • Prefer constructor promotion for compact classes
  • Define constants with const for fixed values
  • Use method chaining (fluent interface) for readable APIs

Quick Check

What access modifier makes a property accessible only within the class itself?

Recap: PHP Classes

You've covered PHP Classes. Key points to remember:

  • Use access modifiers to encapsulate state
  • Prefer constructor promotion for compact classes
  • Define constants with const for fixed values
  • Use method chaining (fluent interface) for readable APIs

Frequently asked questions

Is the “Classes, Properties, and Methods” lesson free?

Yes — the full text of “Classes, Properties, and Methods” is free to read here on the web, and the PHP Academy course includes 4 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 “Classes, Properties, and Methods”?

Define a PHP class with 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 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Classes, Properties, and Methods” 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. Classes, Properties, and Methods
  2. Constructors and Destructors
  3. Static Properties and Methods
  4. Magic Methods Overview
← Back to PHP Academy