Classes: Introduction

Classes provide a way to group functionality and state together.

To define a class, use the class keyword.

class Counter {
  private int $i = 0;

  public function increment(): void {
    $this->i += 1;
  }

  public function get(): int {
    return $this->i;
  }
}

To create an instance of a class, use new, e.g. new Counter();.

Was This Page Useful?
Thank You!
Thank You! If you'd like to share more feedback, please file an issue.