note 76582 added to language.operators.type
| From: | julienpleeusinggmaildotcom at osu1 dot php dot net | Date: | Sat, 21 Jul 2007 01:56:41 +0000 |
| Subject: | note 76582 added to language.operators.type | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-128788@lists.php.net to get a copy of this message | ||
Response to vinyanov at poczta dot onet dot pl:
You mentionned "the instanceof operator will not accept a string as its first operand".
However, this behavior is absolutely right and therefore, you're misleading the meaning of an
instance.
<?php 'ClassA' instanceof 'ClassB'; ?> means "the class named ClassA
is an instance of the class named ClassB". This is a nonsense sentence because when you
instanciate a class, you ALWAYS obtain an object. Consequently, you only can ask if an object is an
instance of a class.
I believe asking if "a ClassA belongs to a ClassB" (or "a ClassA is a class of (type)
ClassB") or even "a ClassA is (also) a ClassB" is more appropriate. But the first is
not implemented and the second only works with objects, just like the instanceof operator.
Plus, I just have tested your code and it does absolutely NOT do the same as instanceof (extended to
classes)! I can't advise anyone to reuse it. The use of <?php is_instance_of ($instanceOfA,
'ClassB'); ?> raises a warning "include_once(Object id #1.php) â¦" when
using __autoload (trying to look for $instanceOfA as if it was a class name).
Finally, here is a fast (to me) sample function code to verify if an object or class:
<?php
function kind_of (&$object_or_class, $class)
{
return is_object ($object_or_class) ?
$object_or_class instanceof $class
: (is_subclass_of ($object_or_class $class)
|| strtolower ($object_or_class) == strtolower ($class));
}
?>
----
Server IP: 195.221.21.36
Probable Submitter: 82.241.244.168
----
Manual Page -- http://www.php.net/manual/en/language.operators.type.php
Edit -- https://master.php.net/note/edit/76582
Del: integrated -- https://master.php.net/note/delete/76582/integrated
Del: useless -- https://master.php.net/note/delete/76582/useless
Del: bad code -- https://master.php.net/note/delete/76582/bad+code
Del: spam -- https://master.php.net/note/delete/76582/spam
Del: non-english -- https://master.php.net/note/delete/76582/non-english
Del: in docs -- https://master.php.net/note/delete/76582/in+docs
Del: other reasons-- https://master.php.net/note/delete/76582
Reject -- https://master.php.net/note/reject/76582
Search -- https://master.php.net/manage/user-notes.php