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
is_a
(PHP 4 >= 4.2.0, PHP 5)
is_a — 객체가 이 클래스나 부모 클래스 중 하나인지 확인
설명
bool is_a
( object $object
, string $class_name
)
주어진 object 가 이 클래스나 부모 클래스 중 하나인지 확인합니다.
인수
- object
-
확인할 객체
- class_name
-
클래스명
반환값
객체가 이 클래스나 부모 클래스 중 하나이면 TRUE, 아니면 FALSE를 반환합니다.
변경점
| 버전 | 설명 |
|---|---|
| 5.3.0 | 이 함수는 배제되지 않고, E_STRICT 경고가 발생하지 않습니다. |
| 5.0.0 | is_a() 함수는 instanceof 연산자로 인해 PHP 5에서 배제되었습니다. 이 함수를 호출하면 E_STRICT 경고가 발생합니다. |
예제
Example #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";
}
?>
Example #2 PHP 5에서 instanceof 연산자 사용하기
<?php
if ($WF instanceof WidgetFactory) {
echo 'Yes, $WF is a WidgetFactory';
}
?>
참고
- get_class() - 객체의 클래스명을 반환
- get_parent_class() - 객체나 클래스의 부모 클래스명을 얻습니다
- is_subclass_of() - 이 클래스가 부모 클래스의 자식인지 확인
is_a
p dot scheit at zweipol dot net
16-Jan-2007 09:44
16-Jan-2007 09:44
martin dunisch
13-Feb-2006 08:02
13-Feb-2006 08:02
Workaround for older PHP-Versions:
function is_a($anObject, $aClass) {
return get_class($anObject) == strtolower($aClass)
or is_subclass_of($anObject, $aClass);
}
dead dot screamer at seznam dot cz
06-Feb-2006 07:44
06-Feb-2006 07:44
Why I test if class `A` inherit class `B` or implements interface `C` before I create class `A`?
<?
//That isn't work:
//1. function is_A()
if(is_A('A','B'))$a=new A;
if(is_A('A','C'))$a=new A;
//2. operator instanceOf
if(A instanceOf B)$a=new A;
if(A instanceOf C)$a=new A;
?>
zabmilenko at hotmail dot com
08-Oct-2005 12:18
08-Oct-2005 12:18
Lazy Instantiation using is_a() and php5
<?php
class ObjectA
{
public function print_line($text)
{
print $text . "\n";
}
}
class ObjectB
{
public function ObjectA()
{
static $objecta;
if (!is_a($objecta, 'ObjectA'))
{
$objecta = new ObjectA;
}
return $objecta;
}
}
$obj = new ObjectB;
$obj->ObjectA()->print_line('testing, 1 2 3');
?>
In the above example, ObjectA is not instantiated until needed by ObjectB. Then ObjectB can continually use it's creation as needed without reinstantiating it.
There are other ways, but I like this one :-)
cesoid at yahoo dot com
06-Oct-2005 02:01
06-Oct-2005 02:01
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.
