A comprehensive demonstration of all major features introduced in PHP 8.0, 8.1, 8.2, and 8.3.
This repository contains KitchenSink.php, a single file that demonstrates every significant feature introduced in the PHP 8.x series. It serves as both a learning resource and a quick reference for PHP 8 features.
- PHP 8.3 or higher (to run all features)
- Composer (optional, for dependency management)
Run the demonstration:
php KitchenSink.php- Named Arguments - Call functions with named parameters
- Union Types - Declare multiple possible types for properties and parameters
- Match Expression - More powerful alternative to switch statements
- Nullsafe Operator (
?->) - Safely access properties and methods on potentially null objects - Constructor Property Promotion - Simplified property declaration in constructors
- Attributes - Native annotations for classes and methods
- New String Functions -
str_contains(),str_starts_with(),str_ends_with() - Weak Maps - Store objects as keys without preventing garbage collection
- Mixed Type - Explicit mixed type declaration
- Throw Expression - Use throw in expressions
- Non-capturing Catches - Catch exceptions without storing them
- Enumerations - First-class support for enums
- Readonly Properties - Properties that can only be initialized once
- First-class Callable Syntax - Create callables from functions using
... - New in Initializers - Use new expressions in parameter defaults
- Pure Intersection Types - Require multiple interface implementations
- Never Return Type - Indicate functions that never return
- Array Unpacking with String Keys - Spread operator for associative arrays
- Fibers - Lightweight cooperative multitasking
- array_is_list() - Check if an array is a list
- Readonly Classes - Make all properties readonly by default
- Disjunctive Normal Form Types - Complex type combinations with AND and OR
- null, false, and true as Standalone Types - Use these as distinct types
- Constants in Traits - Define constants within traits
- Dynamic Properties Deprecated - Requires
#[AllowDynamicProperties]attribute - New Random Extension - Object-oriented random number generation
- Sensitive Parameters - Redact sensitive data in stack traces
- Fetch Enum Properties - Access enum cases and values
- Typed Class Constants - Add types to class constants
- Dynamic Class Constant Fetch - Access constants using variables
- #[Override] Attribute - Explicitly mark method overrides
- Deep Cloning of Readonly Properties - Modify readonly properties during cloning
- json_validate() - Validate JSON without decoding
- Randomizer::getBytesFromString() - Generate random strings from character sets
- Randomizer::getFloat() - Generate random floating-point numbers
- Multi-file Linting - Lint multiple PHP files at once
- Static Return Type - Return type refers to the actual class
- Stringable Interface - Interface for objects convertible to strings
- get_debug_type() - Enhanced type information for debugging
- Resource to Object Migration - Resources converted to objects
- WeakReference - Create weak references to objects
- JIT Compilation - Just-In-Time compilation for performance
- Improved Error Handling - ValueError and UnhandledMatchError exceptions
The KitchenSink.php file is organized into sections:
- PHP 8.0 Features
- PHP 8.1 Features
- PHP 8.2 Features
- PHP 8.3 Features
- Additional PHP 8 Features
- Deprecations and Removals
- Performance Improvements
- Error Handling Improvements
- Usage Examples
Each feature includes:
- Clear comments explaining the feature
- Working code examples
- Practical usage demonstrations
- Start with PHP 8.0 - Understand the foundational changes
- Progress through versions - Each version builds on the previous
- Run the examples - Execute the code to see features in action
- Modify and experiment - Change the code to deepen understanding
- Use named arguments for better readability
- Prefer match expressions over complex switch statements
- Leverage enums for type-safe constants
- Use readonly properties for immutable data
- Apply attributes for metadata and configuration
- Utilize union types for flexible type declarations
- Some features require specific PHP versions to run
- JIT compilation requires OPcache configuration
- Error examples are caught to prevent script termination
- All examples are self-contained and runnable
Feel free to suggest additional examples or improvements to existing demonstrations.
This educational resource is provided as-is for learning purposes.