I have two classes, A and B. B extends class A.
If I execute instance of A
on an instance of B
, it returns TRUE
.
How can this be avoided?
I have two classes, A and B. B extends class A.
If I execute instance of A
on an instance of B
, it returns TRUE
.
How can this be avoided?
This is how instanceof
works, and correctly so: if B inherits from A, it is also of type A.
However, you can check the precise class by using get_class()
instead:
if (get_class($b_instance) == 'A') {
// Not true
}
You need to check class by get_class function
if( get_class($Binst) == 'A' ) {
}
You may want to know the Class name of an object.
Try to use get_class($Object)
.
Example:
$objA = new A(); $objB = new B();
get_class($objA)
will return "A".
and
get_class($objB)
will return "B".
Then if you use
if (get_class($objB) == "A") { echo "Its a A"; }