Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between is_a and instanceof?

Tags:

php

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?

like image 579
Daniel Avatar asked Jun 10 '10 19:06

Daniel


People also ask

How to use instanceof in PHP?

Definition and UsageThe instanceof keyword is used to check if an object belongs to a class. The comparison returns true if the object is an instance of the class, it returns false if it is not.

What is instanceof in java?

The instanceof operator in Java is used to check whether an object is an instance of a particular class or not. Its syntax is. objectName instanceOf className; Here, if objectName is an instance of className , the operator returns true . Otherwise, it returns false .

What is is_ a?

In knowledge representation, object-oriented programming and design (see object-oriented program architecture), is-a (is_a or is a) is a subsumption relationship between abstractions (e.g. types, classes), wherein one class A is a subclass of another class B (and so B is a superclass of A).

What is check instance?

instanceof is a binary operator we use to test if an object is of a given type. The result of the operation is either true or false. It's also known as a type comparison operator because it compares the instance with the type. Before casting an unknown object, the instanceof check should always be used.


2 Answers

Update

As of PHP 5.3.9, the functionality of is_a() has changed. The original answer below states that is_a() must accept an Object as the first argument, but PHP versions >= 5.3.9 now accept an optional third boolean argument $allow_string (defaults to false) to allow comparisons of string class names instead:

class MyBaseClass {} class MyExtendingClass extends MyBaseClass {}  // Original behavior, evaluates to false. is_a(MyExtendingClass::class, MyBaseClass::class);  // New behavior, evaluates to true. is_a(MyExtendingClass::class, MyBaseClass::class, true); 

The key difference in the new behavior between instanceof and is_a() is that instanceof will always check that the target is an instantiated object of the specified class (including extending classes), whereas is_a() only requires that the object be instantiated when the $allow_string argument is set to the default value of false.


Original

Actually, is_a is a function, whereas instanceof is a language construct. is_a will 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_a being a function takes an object as parameter 1, and a string (variable, constant, or literal) as parameter 2. So:

is_a($object, $string); // <- Only way to call it 

instanceof takes 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.

$object instanceof $string;      // <- string class name $object instanceof $otherObject; // <- object instance $object instanceof ClassName;    // <- identifier for the class 
like image 66
ircmaxell Avatar answered Sep 28 '22 12:09

ircmaxell


Here is performance results of is_a() and instanceof:

Test name       Repeats         Result          Performance      instanceof      10000           0.028343 sec    +0.00% is_a()          10000           0.043927 sec    -54.98% 

Test source is here.

like image 39
Alexander Yancharuk Avatar answered Sep 28 '22 12:09

Alexander Yancharuk