File: type.xml

package info (click to toggle)
php-doc 20241205~git.dfcbb86%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 70,956 kB
  • sloc: xml: 968,269; php: 23,883; javascript: 671; sh: 177; makefile: 37
file content (268 lines) | stat: -rw-r--r-- 5,742 bytes parent folder | download
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?xml version="1.0" encoding="utf-8"?>
<sect1 xml:id="language.operators.type">
 <title>Type Operators</title>
 <titleabbrev>Type</titleabbrev>
 <para>
  <literal>instanceof</literal> is used to determine whether a PHP variable
  is an instantiated object of a certain
  <link linkend="language.oop5.basic.class">class</link>:
  <example>
   <title>Using <literal>instanceof</literal> with classes</title>
   <programlisting role="php">
<![CDATA[
<?php
class MyClass
{
}

class NotMyClass
{
}
$a = new MyClass;

var_dump($a instanceof MyClass);
var_dump($a instanceof NotMyClass);
?>
]]>
   </programlisting>
   &example.outputs;
   <screen>
<![CDATA[
bool(true)
bool(false)
]]>
   </screen>
  </example>
 </para>
 <para>
  <literal>instanceof</literal> can also be used to determine whether a variable
  is an instantiated object of a class that inherits from a parent class:
  <example>
   <title>Using <literal>instanceof</literal> with inherited classes</title>
   <programlisting role="php">
<![CDATA[
<?php
class ParentClass
{
}

class MyClass extends ParentClass
{
}

$a = new MyClass;

var_dump($a instanceof MyClass);
var_dump($a instanceof ParentClass);
?>
]]>
   </programlisting>
   &example.outputs;
   <screen>
<![CDATA[
bool(true)
bool(true)
]]>
   </screen>
  </example>
 </para>
 <para>
  To check if an object is <emphasis>not</emphasis> an instanceof a class, the
  <link linkend="language.operators.logical">logical <literal>not</literal>
   operator</link> can be used.
  <example>
   <title>Using <literal>instanceof</literal> to check if object is <emphasis>not</emphasis> an
    instanceof a class</title>
   <programlisting role="php">
<![CDATA[
<?php
class MyClass
{
}

$a = new MyClass;
var_dump(!($a instanceof stdClass));
?>
]]>
   </programlisting>
   &example.outputs;
   <screen>
<![CDATA[
bool(true)
]]>
   </screen>
  </example>
 </para>
 <para>
  Lastly, <literal>instanceof</literal> can also be used to determine whether
  a variable is an instantiated object of a class that implements an
  <link linkend="language.oop5.interfaces">interface</link>:
  <example>
   <title>Using <literal>instanceof</literal> with interfaces</title>
   <programlisting role="php">
<![CDATA[
<?php
interface MyInterface
{
}

class MyClass implements MyInterface
{
}

$a = new MyClass;

var_dump($a instanceof MyClass);
var_dump($a instanceof MyInterface);
?>
]]>
   </programlisting>
   &example.outputs;
   <screen>
<![CDATA[
bool(true)
bool(true)
]]>
   </screen>
  </example>
 </para>
 <para>
  Although <literal>instanceof</literal> is usually used with a literal classname,
  it can also be used with another object or a string variable:
  <example>
   <title>Using <literal>instanceof</literal> with other variables</title>
   <programlisting role="php">
<![CDATA[
<?php
interface MyInterface
{
}

class MyClass implements MyInterface
{
}

$a = new MyClass;
$b = new MyClass;
$c = 'MyClass';
$d = 'NotMyClass';

var_dump($a instanceof $b); // $b is an object of class MyClass
var_dump($a instanceof $c); // $c is a string 'MyClass'
var_dump($a instanceof $d); // $d is a string 'NotMyClass'
?>
]]>
   </programlisting>
   &example.outputs;
   <screen>
<![CDATA[
bool(true)
bool(true)
bool(false)
]]>
   </screen>
  </example>
 </para>
 <para>
  instanceof does not throw any error if the variable being tested is not
  an object, it simply returns &false;. Constants, however, were not allowed
  prior to PHP 7.3.0.
  <example>
   <title>Using <literal>instanceof</literal> to test other variables</title>
   <programlisting role="php">
<![CDATA[
<?php
$a = 1;
$b = NULL;
$c = imagecreate(5, 5);
var_dump($a instanceof stdClass); // $a is an integer
var_dump($b instanceof stdClass); // $b is NULL
var_dump($c instanceof stdClass); // $c is a resource
var_dump(FALSE instanceof stdClass);
?>
]]>
   </programlisting>
   &example.outputs;
   <screen>
<![CDATA[
bool(false)
bool(false)
bool(false)
PHP Fatal error:  instanceof expects an object instance, constant given
]]>
   </screen>
  </example>
 </para>
 <para>
  As of PHP 7.3.0, constants are allowed on the left-hand-side of the
  <literal>instanceof</literal> operator.
  <example>
   <title>Using <literal>instanceof</literal> to test constants</title>
   <programlisting role="php">
<![CDATA[
<?php
var_dump(FALSE instanceof stdClass);
?>
]]>
   </programlisting>
   &example.outputs.73;
   <screen>
<![CDATA[
bool(false)
]]>
   </screen>
  </example>
 </para>
 <para>
  As of PHP 8.0.0, <literal>instanceof</literal> can now be used with arbitrary expressions.
  The expression must be wrapped in parentheses and produce a <type>string</type>.
  <!-- RFC: https://wiki.php.net/rfc/variable_syntax_tweaks -->
  <example>
   <title>Using <literal>instanceof</literal> with an arbitrary expression</title>
   <programlisting role="php">
<![CDATA[
<?php

class ClassA extends \stdClass {}
class ClassB extends \stdClass {}
class ClassC extends ClassB {}
class ClassD extends ClassA {}

function getSomeClass(): string
{
    return ClassA::class;
}

var_dump(new ClassA instanceof ('std' . 'Class'));
var_dump(new ClassB instanceof ('Class' . 'B'));
var_dump(new ClassC instanceof ('Class' . 'A'));
var_dump(new ClassD instanceof (getSomeClass()));
?>
]]>
   </programlisting>
   &example.outputs.8;
   <screen>
<![CDATA[
bool(true)
bool(true)
bool(false)
bool(true)
]]>
   </screen>
  </example>
 </para>
 <simpara>
  The <literal>instanceof</literal> operator has a functional variant
  with the <function>is_a</function> function.
 </simpara>

 <sect2 role="seealso">
  &reftitle.seealso;
  <para>
   <simplelist>
    <member><function>get_class</function></member>
    <member><function>is_a</function></member>
   </simplelist>
  </para>
 </sect2>
</sect1>