Classes and Objects are used to to make your code more efficient and less repetitive by grouping similar tasks.
A class is used to define the actions and data structure used to build objects. The objects are then built using this predefined structure.
class <ClassName> [ extends <ParentClassName> ] [ implements <Interface1> [, <Interface2>, ... ] { } // Class declarationinterface <InterfaceName> [ extends <ParentInterface1> [, <ParentInterface2>, ...] ] { } // Interface declarationuse <Trait1> [, <Trait2>, ...]; // Use traits[ public | protected | private ] [ static ] $<varName>; // Attribute declarationconst <CONST_NAME>; // Constant declaration[ public | protected | private ] [ static ] function <methodName>([args...]) { } // Method declarationClasses may have properties, constants and methods.
class Foo {
private $foo = 'foo'; // OK
private $baz = array(); // OK
private $bar = new Bar(); // Error!
}
Interfaces cannot have properties, but may have constants and methods.
interface FooBar {
const FOO_VALUE = 'bla';
public function doAnything();
}