Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

PHP typed parameters (formally known as type declarations) are explicit constraints applied to function or method signatures that dictate the expected data type of incoming arguments. When a parameter is typed, the PHP engine enforces type checking at runtime, throwing a TypeError exception if the provided argument does not match the declared type or cannot be safely coerced into it.
function functionName(Type $parameterName) {
    // ...
}

Type Resolution and Coercion

PHP handles typed parameters in two distinct modes:
  1. Coercive Mode (Default): PHP utilizes weak typing. If an argument’s type does not match the declaration, PHP will attempt implicit type coercion to cast the value to the expected type without data loss (e.g., casting the string "123" to an int).
  2. Strict Mode: Enabled by declaring declare(strict_types=1); as the very first statement in a file. In strict mode, PHP disables type coercion. The engine requires an exact type match for all function calls made within that file, with the single exception that an int can be passed to a float parameter.

Supported Type Declarations

PHP supports a comprehensive type system for parameters, categorized as follows:
  • Scalar Types: int, float, string, bool.
  • Compound Types: array, iterable, object.
  • Class/Interface Types: Any valid class or interface name (e.g., DateTime, MyCustomInterface).
  • Special Types:
    • callable: Accepts any valid PHP callable (functions, object methods, static class methods, closures).
    • mixed (PHP 8.0+): Represents the union of object|resource|array|string|float|int|bool|null.

Advanced Type Constructs

Modern PHP introduces complex type definitions for parameters: Nullable Types Prefixing a type declaration with a question mark (?) allows the parameter to accept either the specified type or null.
function setIdentifier(?string $id) {}
Union Types (PHP 8.0+) Allows a parameter to accept one of multiple specified types, separated by a pipe (|).
function processInput(int|float|string $input) {}
Intersection Types (PHP 8.1+) Requires an object argument to satisfy multiple class or interface constraints simultaneously, separated by an ampersand (&). This is exclusively for class/interface types.
function iterateCollection(Traversable&Countable $collection) {}
Disjunctive Normal Form (DNF) Types (PHP 8.2+) Allows the combination of Union and Intersection types within a single parameter declaration. Intersection types must be grouped with parentheses.
function handleData((Traversable&Countable)|array $data) {}

Interaction with Default Values

When a typed parameter is assigned a default value, the default value must strictly satisfy the declared type. Historically, assigning a default value of null implicitly made the parameter nullable. However, as of PHP 8.4, this implicit nullability is deprecated; developers must explicitly use the ? prefix or include null in a union type.
// Valid
function configure(bool $isVerbose = false) {}

// Valid (Explicit nullability)
function setLimit(?int $limit = null) {}

// Deprecated in PHP 8.4+ (Implicit nullability)
function setOffset(int $offset = null) {}
Master PHP with Deep Grasping Methodology!Learn More