|-- Indentation
|-- Markup (missing some <para>s)
- Cosmetic (add blank lines in examples)


git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@258288 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Felipe Pena 2008-04-23 03:44:08 +00:00
parent 09ee4e0cc2
commit 141c821c05

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.123 $ -->
<!-- $Revision: 1.124 $ -->
<chapter xml:id="language.operators" xmlns="http://docbook.org/ns/docbook">
<title>Operators</title>
<simpara>
@ -1192,11 +1192,12 @@ var_dump($a === $b); // bool(false)
<example>
<title>Using instanceof with classes</title>
<programlisting role="php">
<![CDATA[
<![CDATA[
<?php
class MyClass
{
}
class NotMyClass
{
}
@ -1205,44 +1206,50 @@ $a = new MyClass;
var_dump($a instanceof MyClass);
var_dump($a instanceof NotMyClass);
?>
]]>
</programlisting>
&example.outputs;
<screen>
]]>
</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 instanceof with inherited classes</title>
<programlisting role="php">
<![CDATA[
<![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>
]]>
</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.
@ -1255,6 +1262,7 @@ bool(true)
class MyClass
{
}
$a = new MyClass;
var_dump(!($a instanceof stdClass));
?>
@ -1267,29 +1275,33 @@ 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 instanceof for class</title>
<programlisting role="php">
<![CDATA[
<![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>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
bool(true)
bool(true)
@ -1303,23 +1315,26 @@ bool(true)
<example>
<title>Using instanceof with other variables</title>
<programlisting role="php">
<![CDATA[
<![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>
@ -1340,7 +1355,7 @@ bool(false)
<example>
<title>Avoiding classname lookups and fatal errors with instanceof in PHP 5.0</title>
<programlisting role="php">
<![CDATA[
<![CDATA[
<?php
$d = 'NotMyClass';
var_dump($a instanceof $d); // no fatal error here