Glossary
Data type
A data type gives a value technical meaning. It helps distinguish a number from text, a list from an object, and a missing value from a valid zero.
Short definition
A type is a technical contract, not a complete business rule.
A data type defines the permitted kind of value and the operations that can be performed on it safely. An integer is added differently from a string, an object can provide methods, and null represents a separate state in which a value is absent. Types can reveal earlier that a function received only an order identifier instead of an order, or that an amount has been confused with formatted text.
PHP is a dynamically typed language: a value’s type is generally determined at runtime. At the same time, it supports type declarations for parameters, return values, and properties that the runtime enforces. Static analysis can find further inconsistencies before execution. But even a precise technical type cannot express everything: int does not guarantee a positive quantity, and string does not guarantee a valid email address or valid user permissions.
What it is used for
Clarify what may enter a calculation or interface.
Types make the boundaries of functions, objects, and API adapters more precise. The reader does not have to guess whether a value is text, a number, a list, or a specific domain object.
- typed parameters and return values of functions or methods
- object properties such as a customer, monetary amount, or order state
- distinguishing a nullable value from required data
- converting JSON and HTTP input into validated domain data
- static contract analysis with PHPStan and automated tests
Practical example
A technical contract for an order number
The function accepts a text prefix and an integer and returns a textual order number. The types guard against a mistake such as passing an array instead of a number; they do not say whether the number belongs to the user or whether the order is in the correct tenant.
In a domain with more rules, a value object may be better than a pair of primitive types. A monetary amount, for example, has a currency, precision, and addition rules as well as a number.
PHP
function formatOrderNumber(string $prefix, int $number): string
{
return sprintf('%s-%06d', $prefix, $number);
}
$orderNumber = formatOrderNumber('WEB', 42);
// WEB-000042
How a type works
From a boundary value to a more precise contract
A type is most valuable at boundaries where data changes from an untrusted or generic format into a clear application model.
- Value arrives An HTTP request, JSON payload, database, or another service provides generic data.
- Parsing and validation The application verifies format, meaning, and required business rules; a simple type is only one layer.
- Typed contract A function or object accepts the value in its declared technical form.
- Working with the value The code can use operations that make sense for the type, while tools check for inconsistencies.
- Output The result receives its own type, which the next part of the application uses as a contract again.
Important concepts
A value, type declaration, and validation have different roles.
Confusing these layers leads either to a false sense of security or to overly generic code full of mixed values.
Scalar types
bool, int, float, and string represent simple values. A float is not automatically suitable for a monetary amount, where precision and unit must be known.
Composite and user-defined types
array, object, class, interface, and enum describe more structured values. An array without a description of its shape is often insufficiently specific.
null and nullable types
null means the absence of a value. ?Customer can express that the customer is not yet known; it is not the same as an empty object or ID 0.
Union and intersection
A union permits several alternatives, such as string|int. An intersection requires an object to satisfy several contracts at once. They should be used only where they match the actual model.
PHPDoc and analysis
PHPDoc can make a collection shape or generic type more precise for tools. PHPStan analyses it, but the PHP runtime does not normally enforce it itself.
Benefits and limitations
More precise types improve communication; they do not remove the need for design.
Benefits
- a more readable contract for functions, objects, and adapters
- earlier detection of confused values through runtime checks and static analysis
- safer refactoring when the data model changes
- less need to infer a value’s shape from its variable name
Common mistakes
- overly generic mixed or array without a description of its contents
- using string for a price, date, email, and identifier without any further model
- assuming that int guarantees a positive and permitted business value
- complex union types that obscure an unclear model instead of representing a genuine variant
Practical boundary
Use types primarily where values are passed on and change meaning.
Type declarations provide the greatest benefit on public methods, constructor parameters, return values, and data objects. Within a short calculation, the type may be clear from the context, but naming and a simple local structure remain important.
When two primitive types carry different meanings, such as an amount in cents and a number of items, it is worth distinguishing them by name, validation, and—depending on complexity—a custom value object. A type is neither an end in itself nor a substitute for understanding the domain.
What to consider
Types should capture meaning at the right boundary.
The most useful type contract is precise enough for the rule at hand without locking the application into artificial abstractions.
- declare parameters, return values, and properties with public significance
- validate business conditions that the type cannot express
- do not use float for monetary amounts without a clear reason
- limit mixed and generic arrays to integration boundaries, then convert them into a more precise model
- combine declarations, PHPDoc, and PHPStan with tests of real behaviour
Common questions
Data types in practice
Is PHP a statically typed language?
No. PHP is dynamically typed: a value’s type is generally determined at runtime. Type declarations can nevertheless make the runtime check parameters, return values, and properties.
Is null zero or empty text?
No. null represents the absence of a value. Zero and an empty string are specific values with their own meaning.
Does a data type replace validation?
No. The string type does not verify that an email address is valid, and int does not verify that a quantity is positive. Validation adds meaning and business rules.
Is PHPDoc the same as a runtime type?
No. PHPDoc helps people and tools such as PHPStan. The normal PHP runtime does not automatically enforce it like a parameter or return type declaration.
How I work with PHP in practice
I use types to create readable and safe application boundaries.
In PHP projects, I combine declared types, PHPStan, tests, and domain rules so that a change to data does not cause a silent error elsewhere in the system.