I am aware that instanceof is an operator and that is_a is a method.
Is the method slower in performance? What would you prefer to use?
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.
Update
As of PHP 5.3.9, the functionality of
is_a()has changed. The original answer below states thatis_a()must accept anObjectas the first argument, but PHP versions >= 5.3.9 now accept an optional third boolean argument$allow_string(defaults tofalse) to allow comparisons of string class names instead:The key difference in the new behavior between
instanceofandis_a()is thatinstanceofwill always check that the target is an instantiated object of the specified class (including extending classes), whereasis_a()only requires that the object be instantiated when the$allow_stringargument is set to the default value offalse.Original
Actually,
is_ais a function, whereasinstanceofis a language construct.is_awill be significantly slower (since it has all the overhead of executing a function call), but the overall execution time is minimal in either method.It’s no longer deprecated as of 5.3, so there’s no worry there.
There is one difference however.
is_abeing a function takes an object as parameter 1, and a string (variable, constant, or literal) as parameter 2. So:instanceoftakes an object as parameter 1, and can take a class name (variable), object instance (variable), or class identifier (class name written without quotes) as parameter 2.