Object-oriented and modern PHP
The object model in one run: classes and objects, the constructor, inheritance, interfaces and traits, enums, the magic methods and attributes, exceptions, strict_types, closures and generators. The PHP OOP section holds the longer examples of the same ground.
5.1
Classes and objects
define a class, create objects, add methods, and what $this is
Open
5.2
Constructors and promotion
__construct, constructor promotion, defaults and readonly properties
Open
5.3
Inheritance
extends, parent calls, visibility and abstract classes
Open
5.4
Interfaces and traits
interfaces, implements, traits, and when to use each one
Open
5.5
Enums
enums, backed enums, methods on an enum, and match over one
Open
5.6
Magic methods
__toString, __invoke, __get and __set, and when they earn their place
Open
5.7
Attributes
add metadata with attributes and read it back with reflection
Open
5.8
Exceptions
throw, catch by type, and clean up with finally
Open
5.9
Strict types
strict_types and the exact scalar and return types it enforces
Open
5.10
Closures and arrow functions
anonymous functions, captured scope, and a callable passed in
Open
5.11
Generators with yield
yield a large sequence without loading an array into memory
Open