instanceof
is not of valid type PHP-E1009A class that you are trying to check against using instanceof
keyword is not of valid class object type. This will result in fatal runtime error.
This can happen because of human errors, and it is recommended to make sure that:
self
/parent
is not being used outside of a class scope.class ObjectChecker
{
public function isInstanceOfBaseGreet($object): bool
{
if ($object instanceof BaseGreet) { // BaseGreet class doesn't exists
return true;
}
return false;
}
}
class BaseGreet {}
class ObjectChecker
{
public function isInstanceOfBaseGreet($object): bool
{
if ($object instanceof BaseGret) { // typo, BaseGret not exists
return true;
}
return false;
}
}
function isInstanceOf($object) {
if ($object instanceof self) { // invalid: Cannot use "self" when no class scope is active
return true;
}
return false;
}
Make sure class/interface you are trying to check against exists and doesn't have any typo:
class BaseGreet
{
public function getMessage(): string
{
return 'Hello';
}
}
class ObjectChecker
{
public function isInstanceOfBaseGreet($object): bool
{
if ($object instanceof BaseGreet) {
return true;
}
return false;
}
}
Make sure there is not typo in class/interface name:
class BaseGreet {}
class ObjectChecker
{
public function isInstanceOfBaseGreet($object): bool
{
if ($object instanceof BaseGreet) {
return true;
}
return false;
}
}
Always use self
/parent
inside class scope:
class ObjectChecker
{
public function isInstanceOfObjectChecker($object): bool
{
if ($object instanceof self) {
return true;
}
return false;
}
}