GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Downloads
  • Documentation
  • Get Involved
  • Help
  • Getting Started
  • Introduction
  • A simple tutorial
  • Language Reference
  • Basic syntax
  • Types
  • Variables
  • Constants
  • Expressions
  • Operators

This view

  • Original page
  • New address

GeezerVison is showing www.php.net — PHP: Type System - Manual

PHP 8.6.0 RC5 available for testing
    NULL »
    « Introduction
    • Preface
    • Language Reference
    • Types

    Type System

    PHP uses a nominal type system with a strong behavioral subtyping relation. The subtyping relation is checked at compile time whereas the verification of types is dynamically checked at run time.

    PHP's type system supports various atomic types that can be composed together to create more complex types. Some of these types can be written as type declarations.

    Atomic types

    Some atomic types are built-in types which are tightly integrated with the language and cannot be reproduced with user-defined types.

    The list of base types is:

    • Built-in types

      • Scalar types:

        • bool type

        • int type

        • float type

        • string type

      • array type

      • object type

      • resource type

      • never type

      • void type

      • Relative class types: self, parent, and static

      • Singleton types

        • false

        • true

      • Unit types

        • null

    • User-defined types (generally referred to as class-types)

      • Interfaces

      • Classes

      • Enumerations

    • callable type

    Scalar types

    A value is considered scalar if it is of type int, float, string or bool.

    User-defined types

    It is possible to define custom types with interfaces, classes and enumerations. These are considered as user-defined types, or class-types. For example, a class called Elephant can be defined, then objects of type Elephant can be instantiated, and a function can request a parameter of type Elephant.

    Composite types

    It is possible to combine multiple atomic types into composite types. PHP allows types to be combined in the following ways:

    • Intersection of class-types (interfaces and class names).

    • Union of types.

    Intersection types

    An intersection type accepts values which satisfy multiple class-type declarations, rather than a single one. Individual types which form the intersection type are joined by the & symbol. Therefore, an intersection type comprised of the types T, U, and V will be written as T&U&V.

    Union types

    A union type accepts values of multiple different types, rather than a single one. Individual types which form the union type are joined by the | symbol. Therefore, a union type comprised of the types T, U, and V will be written as T|U|V. If one of the types is an intersection type, it needs to be bracketed with parentheses for it to be written in DNF: T|(X&Y).

    Type aliases

    PHP supports two type aliases: mixed and iterable which correspond to the union type of object|resource|array|string|float|int|bool|null and Traversable|array respectively.

    Note:

    PHP does not support user-defined type aliases.

    Found A Problem?

    Learn How To Improve This Page • Submit a Pull Request • Report a Bug
    +add a note

    User Contributed Notes

    There are no user contributed notes for this page.
    • Types
      • Introduction
      • Type System
      • NULL
      • Booleans
      • Integers
      • Floating-​point numbers
      • Strings
      • Numeric strings
      • Arrays
      • Objects
      • Enumerations
      • Resources
      • Callables
      • Mixed
      • Void
      • Never
      • Relative class types
      • Singleton types
      • Iterables
      • Type declarations
      • Type Juggling
    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.