mirror of
https://github.com/php/doc-en.git
synced 2026-03-23 23:32:18 +01:00
227 lines
6.8 KiB
XML
227 lines
6.8 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<sect1 xml:id="language.types.type-system">
|
|
<title>Type System</title>
|
|
|
|
<para>
|
|
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.
|
|
</para>
|
|
|
|
<para>
|
|
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
|
|
<link linkend="language.types.declarations">type declarations</link>.
|
|
</para>
|
|
|
|
<sect2 xml:id="language.types.type-system.atomic">
|
|
<title>Atomic types</title>
|
|
<para>
|
|
Some atomic types are built-in types which are tightly integrated with the
|
|
language and cannot be reproduced with user defined types.
|
|
</para>
|
|
|
|
<para>
|
|
The list of base types is:
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>Built-in types</simpara>
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>
|
|
Scalar types:
|
|
</simpara>
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara><type>bool</type> type</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara><type>int</type> type</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara><type>float</type> type</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara><type>string</type> type</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara><type>array</type> type</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara><type>object</type> type</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara><type>resource</type> type</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara><type>never</type> type</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara><type>void</type> type</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
<link linkend="language.types.relative-class-types">Relative class types</link>:
|
|
<type>self</type>, <type>parent</type>, and <type>static</type>
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
<link linkend="language.types.singleton">Singleton types</link>
|
|
</simpara>
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara><type>false</type></simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara><type>true</type></simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Unit types
|
|
</simpara>
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara><type>null</type></simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
</listitem>
|
|
</itemizedlist>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
User-defined types (generally referred to as class-types)
|
|
</simpara>
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara><link linkend="language.oop5.interfaces">Interfaces</link></simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara><link linkend="language.oop5.basic.class">Classes</link></simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara><link linkend="language.types.enumerations">Enumerations</link></simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara><type>callable</type> type</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
</para>
|
|
|
|
<sect3 xml:id="language.types.type-system.atomic.scalar">
|
|
<title>Scalar types</title>
|
|
<simpara>
|
|
A value is considered scalar if it is of type <type>int</type>,
|
|
<type>float</type>, <type>string</type> or <type>bool</type>.
|
|
</simpara>
|
|
</sect3>
|
|
|
|
<sect3 xml:id="language.types.type-system.atomic.user-defined">
|
|
<title>User-defined types</title>
|
|
<simpara>
|
|
It is possible to define custom types with
|
|
<link linkend="language.oop5.interfaces">interfaces</link>,
|
|
<link linkend="language.oop5.basic.class">classes</link> and
|
|
<link linkend="language.types.enumerations">enumerations</link>.
|
|
These are considered as user-defined types, or class-types.
|
|
For example, a class called <literal>Elephant</literal> can be defined,
|
|
then objects of type <literal>Elephant</literal> can be instantiated,
|
|
and a function can request a parameter of type <literal>Elephant</literal>.
|
|
</simpara>
|
|
</sect3>
|
|
</sect2>
|
|
|
|
<sect2 xml:id="language.types.type-system.composite">
|
|
<title>Composite types</title>
|
|
<para>
|
|
It is possible to combine multiple atomic types into composite types.
|
|
PHP allows types to be combined in the following ways:
|
|
</para>
|
|
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>
|
|
Intersection of class-types (interfaces and class names).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Union of types.
|
|
</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
|
|
<sect3 xml:id="language.types.type-system.composite.intersection">
|
|
<title>Intersection types</title>
|
|
<para>
|
|
An intersection type accepts values which satisfies multiple
|
|
class-type declarations, rather than a single one.
|
|
Individual types which form the intersection type are joined by the
|
|
<literal>&</literal> symbol. Therefore, an intersection type comprised
|
|
of the types <literal>T</literal>, <literal>U</literal>, and
|
|
<literal>V</literal> will be written as <literal>T&U&V</literal>.
|
|
</para>
|
|
</sect3>
|
|
|
|
<sect3 xml:id="language.types.type-system.composite.union">
|
|
<title>Union types</title>
|
|
<para>
|
|
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
|
|
<literal>|</literal> symbol. Therefore, a union type comprised
|
|
of the types <literal>T</literal>, <literal>U</literal>, and
|
|
<literal>V</literal> will be written as <literal>T|U|V</literal>.
|
|
If one of the types is an intersection type, it needs to be bracketed
|
|
with parenthesis for it to written in <acronym>DNF</acronym>:
|
|
<literal>T|(X&Y)</literal>.
|
|
</para>
|
|
</sect3>
|
|
</sect2>
|
|
|
|
<sect2 xml:id="language.types.type-system.alias">
|
|
<title>Type aliases</title>
|
|
|
|
<para>
|
|
PHP supports two type aliases: <type>mixed</type> and
|
|
<type>iterable</type> which corresponds to the
|
|
<link linkend="language.types.type-system.composite.union">union type</link>
|
|
of <literal>object|resource|array|string|float|int|bool|null</literal>
|
|
and <literal>Traversable|array</literal> respectively.
|
|
</para>
|
|
|
|
<note>
|
|
<simpara>
|
|
PHP does not support user-defined type aliases.
|
|
</simpara>
|
|
</note>
|
|
</sect2>
|
|
|
|
</sect1>
|
|
<!-- Keep this comment at the end of the file
|
|
Local variables:
|
|
mode: sgml
|
|
sgml-omittag:t
|
|
sgml-shorttag:t
|
|
sgml-minimize-attributes:nil
|
|
sgml-always-quote-attributes:t
|
|
sgml-indent-step:1
|
|
sgml-indent-data:t
|
|
indent-tabs-mode:nil
|
|
sgml-parent-document:nil
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
|
sgml-exposed-tags:nil
|
|
sgml-local-catalogs:nil
|
|
sgml-local-ecat-files:nil
|
|
End:
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
vim: et tw=78 syn=sgml
|
|
vi: ts=1 sw=1
|
|
-->
|