note 75871 added to language.operators.type
| From: | vinyanov at poczta dot onet dot pl | Date: | Tue, 19 Jun 2007 22:57:49 +0000 |
| Subject: | note 75871 added to language.operators.type | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-127803@lists.php.net to get a copy of this message | ||
Unfortunately the instanceof operator will not accept a string as its first operand. So I wrote this
function. It does exactly the same (ie, successively checks identicalness, inheritance and
implementation). Just on strings.
<?php
function is_instance_of($sub, $super)
{
$sub = (string)$sub;
$super = is_object($super) ? get_class($super) : (string)$super;
switch(true)
{
case $sub === $super; // well ... conformity
case is_subclass_of($sub, $super):
case in_array($super, class_implements($sub)):
return true;
default:
return false;
}
}
// testing
interface X {}
class A {}
class B extends A {}
class C extends B {}
class D implements X {}
$i = 'is_instance_of';
var_dump($i('A', 'A'), $i('B', 'A'), $i('C',
'A'), $i('D', 'X'));
?>
----
Server IP: 195.136.184.34
Probable Submitter: 83.31.205.27
----
Manual Page -- http://www.php.net/manual/en/language.operators.type.php
Edit -- https://master.php.net/note/edit/75871
Del: integrated -- https://master.php.net/note/delete/75871/integrated
Del: useless -- https://master.php.net/note/delete/75871/useless
Del: bad code -- https://master.php.net/note/delete/75871/bad+code
Del: spam -- https://master.php.net/note/delete/75871/spam
Del: non-english -- https://master.php.net/note/delete/75871/non-english
Del: in docs -- https://master.php.net/note/delete/75871/in+docs
Del: other reasons-- https://master.php.net/note/delete/75871
Reject -- https://master.php.net/note/reject/75871
Search -- https://master.php.net/manage/user-notes.php