mabus Posted April 25, 2008 Share Posted April 25, 2008 I usually user php's "assert" function to test variables. Now, is there a way to determine the type of object that is initialized to a variable? The function "is_object" is not good enough, 'cause it will only determine a particular variable or not, but what I want is to determine if a particular object satisfies the set requirement. for example: public function GetSomething(MyDesiredObject $newObject) { ... some implementation here ... } Now, I want this function to be able to accept only that "MyDesiredObject" data type. usually I'd do an assert from within the function to test, but how about if I want to determine if the value of that "$newObject" variable is really of "MyDesiredObject" data type? How do we do this on PHP? Link to comment https://forums.phpfreaks.com/topic/102839-solved-determining-the-type-of-object/ Share on other sites More sharing options...
sasa Posted April 25, 2008 Share Posted April 25, 2008 try <?php class a{ } class b{ } $x = new b; if ($x instanceof a) echo 'is a'; else echo 'it is not a'; ?> Link to comment https://forums.phpfreaks.com/topic/102839-solved-determining-the-type-of-object/#findComment-526835 Share on other sites More sharing options...
mabus Posted April 26, 2008 Author Share Posted April 26, 2008 try <?php class a{ } class b{ } $x = new b; if ($x instanceof a) echo 'is a'; else echo 'it is not a'; ?> This one works, and is good enough for me. Thanks for the help Link to comment https://forums.phpfreaks.com/topic/102839-solved-determining-the-type-of-object/#findComment-527797 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.