Free preview.You're sampling one lesson — enroll free to unlock all 8 lessons and track your progress.
Enroll free
lesson

Classes and Instances

Classes and Instances

In this lesson — part of Objects and Classes — you'll learn classes and instances in PHP and why it matters in real work.

Why it matters

Classes bundle data and behavior together — the core of object-oriented design.

Key ideas

  • Classes and instances
  • Fields and methods
  • Constructors
  • Encapsulation

In practice

Here's how it looks in idiomatic PHP:

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

    public function increment(): int {
        return ++$this->count;       // access via $this->
    }
}

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

PHP note: PHP classes use visibility keywords (public/private/protected) and reference instance members through $this->member; objects are created with new.

Try it yourself

Exercise: In PHP, write a Bank Account class with deposit and withdraw methods.

Recap

You now understand classes and instances and can apply it in PHP. Mark this lesson complete and continue to the next one.