Welcome Guest, Not a member yet? Register   Sign In
Accessing a custom object with instanceof
#9

This is from PHP.net.

Code:
If you want to test if a classname is an instance of a class, the instanceof operator won't work.

<?php
$classname = 'MyClass';
if( $classname instanceof MyParentClass) echo 'Child of it';
else echo 'Not child of it';
?>

Will always output
Not child of it

You must use a ReflectionClass :
<?php
$classname = 'MyClass';
$myReflection = new ReflectionClass($classname);
if( $myReflection->isSubclassOf('MyParentClass')) echo  'Child of it';
else echo 'Not child of it';
?>

Will output the good result.
If you're testing an interface, use implementsInterface() instead of isSublassOf().
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Messages In This Thread
RE: Accessing a custom object with instanceof - by InsiteFX - 01-27-2021, 02:20 AM



Theme © iAndrew 2016 - Forum software by © MyBB