Classes and Objects

suggest change

Versions

The notes below were written for PHP 4.0 through 7.1. Later versions relax some of the initializer rules.

Introduction

Classes and objects group similar data and behavior so the same structure is not repeated.

A class is used to define the actions and data structure used to build objects. The objects are then built using this predefined structure.

Syntax

Remarks

Classes and Interface components

Classes may have properties, constants and methods.

class Foo {
    private $foo = 'foo'; // OK
    private $baz = array(); // OK
    private $bar = new Bar(); // Syntax error before PHP 8.1
}

Interfaces cannot have properties, but may have constants and methods.

interface FooBar {
    const FOO_VALUE = 'bla';
    public function doAnything();
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents