PHP 8.3.14 Released!

is_a

(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)

is_a检查对象是否属于一个给定的类型或子类型。

说明

is_a(mixed $object_or_class, string $class, bool $allow_string = false): bool

确定指定 object_or_class 是否是 class 对象类型,或者是否将 class 作为其中一个超类(父类)。

参数

object_or_class

类名或者实例对象。

class

类名或接口名。

allow_string

如果本参数设置为 falseobject_or_class 就不允许传入字符串类名。 这也会在类不存在时,阻止调用自动加载器(autoloader)。

返回值

如果 object_or_classclass 对象类型,或者 class 是其中一个超类(父类),则返回 true,否则返回 false

示例

示例 #1 is_a() 示例

<?php
// 定义类
class WidgetFactory
{
var
$oink = 'moo';
}

// 创建新对象
$WF = new WidgetFactory();

if (
is_a($WF, 'WidgetFactory')) {
echo
"yes, \$WF is still a WidgetFactory\n";
}
?>

示例 #2 使用 instanceof 运算符

<?php
if ($WF instanceof WidgetFactory) {
echo
'Yes, $WF is a WidgetFactory';
}
?>

参见

添加备注

用户贡献的备注 7 notes

up
66
Ronald Locke
8 years ago
Please note that you have to fully qualify the class name in the second parameter.

A use statement will not resolve namespace dependencies in that is_a() function.

<?php
namespace foo\bar;

class
A {};
class
B extends A {};
?>

<?php
namespace har\var;

use
foo\bar\A;
$foo = new foo\bar\B();

is_a($foo, 'A'); // returns false;
is_a($foo, 'foo\bar\A'); // returns true;
?>

Just adding that note here because all examples are without namespaces.
up
34
Aron Budinszky
13 years ago
Be careful! Starting in PHP 5.3.7 the behavior of is_a() has changed slightly: when calling is_a() with a first argument that is not an object, __autoload() is triggered!

In practice, this means that calling is_a('23', 'User'); will trigger __autoload() on "23". Previously, the above statement simply returned 'false'.

More info can be found here:
https://bugs.php.net/bug.php?id=55475

Whether this change is considered a bug and whether it will be reverted or kept in future versions is yet to be determined, but nevertheless it is how it is, for now...
up
18
p dot scheit at zweipol dot net
17 years ago
At least in PHP 5.1.6 this works as well with Interfaces.

<?php
interface test {
public function
A();
}

class
TestImplementor implements test {
public function
A () {
print
"A";
}
}

$testImpl = new TestImplementor();

var_dump(is_a($testImpl,'test'));
?>

will return true
up
4
SlimDeluxe
2 years ago
For anyone wondering, this does not work with traits :(
up
11
cesoid at yahoo dot com
19 years ago
is_a returns TRUE for instances of children of the class.

For example:

class Animal
{}

class Dog extends Animal
{}

$test = new Dog();

In this example is_a($test, "Animal") would evaluate to TRUE as well as is_a($test, "Dog").

This seemed intuitive to me, but did not seem to be documented.
up
2
dkrupyanskiy[at]gmail
6 years ago
Looks like the function signature given in description is wrong. Actually it can take a string as a first parameter in the case if $allow_string is set to true.

It took some time to find out how the last parameter should be used. Please consider the following example

<?php

class Foo{}

spl_autoload_register(
function(
$classname){
printf('autoload has been triggered for %s%s', $classname, PHP_EOL);
}
);

var_dump(is_a('UndefinedClassName', Foo::class, true));

?>
up
2
eitan at mosenkis dot net
12 years ago
As of PHP 5.3.9, is_a() seems to return false when passed a string for the first argument. Instead, use is_subclass_of() and, if necessary for your purposes, also check if the two arguments are equal, since is_subclass_of('foo', 'foo') will return false, while is_a('foo', 'foo') used to return true.
To Top