note 71398 added to language.operators.type
| From: | thisbizness at gmail dot com | Date: | Mon, 27 Nov 2006 01:19:27 +0000 |
| Subject: | note 71398 added to language.operators.type | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-120095@lists.php.net to get a copy of this message | ||
***
w dot bogaerts at kratz dot nl said:
instanceof only works with objects. So if you want to check if a variable is an object that
implements a certain interface, you need two checks: is_object and instanceof.
A fatal error occurs if instanceof is used with other data types.
***
I don't exactly know what he meant by that because I was able to use instanceof just fine with
other data types.
<?php
class A{
function __construct(){}
}
function test($variable) {
echo ($variable instanceof A) ? "this is an instance of class A<br/>" :
"this is not an object<br/>";
}
$a = new A(); //object
$b = array("a","b","c"); //array
$c = "c"; //string
test($a);
test($b);
test($c);
test("d"); //string again
?>
results:
this is an instance of class A
this is not an object
this is not an object
this is not an object
----
Server IP: 66.207.199.35
Probable Submitter: 216.99.43.20
----
Manual Page -- http://www.php.net/manual/en/language.operators.type.php
Edit -- https://master.php.net/note/edit/71398
Del: integrated -- https://master.php.net/note/delete/71398/integrated
Del: useless -- https://master.php.net/note/delete/71398/useless
Del: bad code -- https://master.php.net/note/delete/71398/bad+code
Del: spam -- https://master.php.net/note/delete/71398/spam
Del: non-english -- https://master.php.net/note/delete/71398/non-english
Del: in docs -- https://master.php.net/note/delete/71398/in+docs
Del: other reasons-- https://master.php.net/note/delete/71398
Reject -- https://master.php.net/note/reject/71398
Search -- https://master.php.net/manage/user-notes.php