Example: A method is supposed to return an instance of a SpecificClass. How can I check that return value if it is from that class?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use the
instanceofoperator, to check if an object is an instance of :Which means that it cannot be used to detect if your object is an instance of a
specificclass — as it will say “yes” if your object is an instance of a child-class of that class.For instance, this portion of code :
Will get you this output :
$a, in a way, is an instance ofClassA, asClassBis a child-class ofClassA.And, of course,
$ais also an instance ofClassB— see the line where it’s instanciated.