note 106882 added to language.operators.type
| From: | SudarshanWadkar at osu1 dot php dot net | Date: | Thu, 15 Dec 2011 12:34:33 +0000 |
| Subject: | note 106882 added to language.operators.type | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-185075@lists.php.net to get a copy of this message | ||
I don't see any mention of "namespaces" on this page so I thought I would chime in.
The instanceof operator takes FQCN as second operator when you pass it as string and not a simple
class name. It will not resolve it even if you have a
use MyNamespace\Bar; at the top
level. Here is what I am trying to say:
## testinclude.php ##
<?php
namespace Bar1;
{
class Foo1{ }
}
namespace Bar2;
{
class Foo2{ }
}
?>
## test.php ##
<?php
include('testinclude.php');
use Bar1\Foo1 as Foo;
$foo1 = new Foo(); $className = 'Bar1\Foo1';
var_dump($foo1 instanceof Bar1\Foo1);
var_dump($foo1 instanceof $className);
$className = 'Foo';
var_dump($foo1 instanceof $className);
use Bar2\Foo2;
$foo2 = new Foo2(); $className = 'Bar2\Foo2';
var_dump($foo2 instanceof Bar2\Foo2);
var_dump($foo2 instanceof $className);
$className = 'Foo2';
var_dump($foo2 instanceof $className);
?>
## stdout ##
bool(true)
bool(true)
bool(false)
bool(true)
bool(true)
bool(false)
----
Server IP: 69.147.83.197
Probable Submitter: 115.249.177.42
----
Manual Page -- http://www.php.net/manual/en/language.operators.type.php
Edit -- https://master.php.net/note/edit/106882
Del: integrated -- https://master.php.net/note/delete/106882/integrated
Del: useless -- https://master.php.net/note/delete/106882/useless
Del: bad code -- https://master.php.net/note/delete/106882/bad+code
Del: spam -- https://master.php.net/note/delete/106882/spam
Del: non-english -- https://master.php.net/note/delete/106882/non-english
Del: in docs -- https://master.php.net/note/delete/106882/in+docs
Del: other reasons-- https://master.php.net/note/delete/106882
Reject -- https://master.php.net/note/reject/106882
Search -- https://master.php.net/manage/user-notes.php