What is the difference between is_a and instanceof?

后端 未结 9 865
难免孤独
难免孤独 2020-12-04 07:23

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?

相关标签:
9条回答
  • 2020-12-04 07:58

    In regards to ChrisF's answer, is_a() is no longer deprecated as of PHP 5.3.0. I find it's always safer to go by the official source for things like this.

    With regards to your question, Daniel, I can't say about the performance differences, but part of it will come down to readibility and which you find easier to work with.

    Also, there is some discussion about the confusion around negating an instanceof check vs is_a(). For example, for instanceof you would do:

    <?php
    if( !($a instanceof A) ) { //... }
    ?>
    

    vs the following for is_a():

    <?php
    if( !is_a($a, 'A' ) { //... }
    ?>
    

    or

    <?php
    if( is_a($a, 'A') === FALSE) { //... }
    ?>
    

    Edit Looks like ChrisF deleted his answer, but the first part of my answer still stands.

    0 讨论(0)
  • 2020-12-04 08:00

    Besides the speed, another important difference is how they handle edge cases.

    is_a($x1, $x2) // fatal error if x2 is not a string nor an object
    $x1 instanceof $x2  // returns false even if $x2 is int, undefined, etc.
    

    So, is_a() highlights possible bugs while instanceof suppresses them.

    0 讨论(0)
  • 2020-12-04 08:02

    The optimisation is minimal. And micro-optimisations are never a real good answer, in front of the readability, understandability and stability of the code.

    ( personaly I prefere instanceof, but the choice is yours ;) )

    The principal difference is the possibility to use direct class name with instanceof

    $a instanceof MyClass

    is shorter than

    is_a($a, MyClass::class)

    ( ok… it’s not trivial. )

    The syntaxical coloration between instanceof (language structure) and is_a is usefull too (for me). letting function color to bigger operations. And for single use in if, instanceof dosn’t need more parenthesis.

    Note : Of course instead of MyClass::class you can use a shorter direct string :

    is_a($a,'MyClass')

    But use direct string in a code isn’t a good practice.

    The syntaxical colloration is better and more usefull if you can make a difference between simple string and classes names. And it's easier to change names with constant classname. Specialy if you use namespace with alias.

    So, wy use is_a() ?

    For same raison : readability and undestandability. (the choice is yours) Specialy when used with ! or others boolean operators : is_a seems more pratical with parenthesis.

    if( $a AND (!is_a ($a, MyClass::class) OR is_a ($a, MyOtherClass::class)) )

    is more readable than :

    if( $a AND (!( $a instanceof MyClass) OR ($a intanceof MyOtherClass)))

    An other good reason is when you need use callback in functions. ( like array_map … ) instanceof isn’t a function, it’s a language construct, so you cannot use it as callback.

    In thoses cases, is_a may be usefull

    0 讨论(0)
提交回复
热议问题