What is the difference between is_a and instanceof?

后端 未结 9 864
难免孤独
难免孤独 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:37

    I can't speak for performance -- I haven't measured anything yet -- but depending on what you are attempting, there are limitations with instanceof. Check out my question, just recently, about it:

    PHP 'instanceof' failing with class constant

    I've ended up using is_a instead. I like the structure of instanceof better (I think it reads nicer) and will continue to use it where I can.

    0 讨论(0)
  • 2020-12-04 07:41

    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.

    0 讨论(0)
  • 2020-12-04 07:48

    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
    
    0 讨论(0)
  • 2020-12-04 07:49

    There is a scenario where only is_a() works and instanceof will fail.

    instanceof expects a literal class name or a variable that is either an object or a string (with the name of a class) as its right argument.

    But if you want to provide the string of a class name from a function call it will not work and result in a syntax error.

    However, the same scenario works fine with is_a().

    Example:

    <?php
    
    function getClassName() : string
    {
        return "Foobar";
    }
    
    class Foobar
    {
        private $xyz;
    
    }
    
    $x = new Foobar();
    
    // this works of course
    var_dump($x instanceof Foobar);
    
    // this creates a syntax error
    var_dump($x instanceof getClassName());
    
    // this works
    var_dump(is_a($x, getClassName()));
    

    This is based on PHP 7.2.14.

    0 讨论(0)
  • 2020-12-04 07:51

    instanceof can be used with other object instances, the class's name, or an interface. I don't think that is_a() works with interfaces (only a string representing a class name), but correct me if it does. (Update: See https://gist.github.com/1455148)

    Example from php.net:

    interface MyInterface
    {
    }
    
    class MyClass implements MyInterface
    {
    }
    
    $a = new MyClass;
    $b = new MyClass;
    $c = 'MyClass';
    $d = 'NotMyClass';
    
    var_dump($a instanceof $b); // $b is an object of class MyClass
    var_dump($a instanceof $c); // $c is a string 'MyClass'
    var_dump($a instanceof $d); // $d is a string 'NotMyClass'
    

    outputs:

    bool(true)
    bool(true)
    bool(false)
    
    0 讨论(0)
  • 2020-12-04 07:56

    Here are performance results obtained from here:

    instanceof is faster.

    Functions

    function method_1($a = null) { 
        return is_object($a) && is_a($a, 'Example');
    }
    
    function method_2($a = null) {
        return is_a((object) $a, 'Example');
    }
    
    function method_3($a = null) {
        return $a instanceof 'Example';
    }
    

    Times (run 5000 times each)

    0.00573397 // method_1(5) 
    0.01437402 // method_2(5) 
    0.00376201 // method_3(5)
    
    0 讨论(0)
提交回复
热议问题