1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
<?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><type>null</type> type</simpara>
</listitem>
<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>
</itemizedlist>
</listitem>
<listitem>
<simpara>
<link linkend="language.types.value">Value types</link>
</simpara>
<itemizedlist>
<listitem>
<simpara><type>false</type></simpara>
</listitem>
<listitem>
<simpara><type>true</type></simpara>
</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
-->
|