• is_object + instanceof#

  • BobRay Reply #1, 2 years, 1 month ago

    Reply
    I've been trying hard, but I can't see why this common code in MODx uses both tests:

    if (is_object($object) && $object instanceof xPDOObject) {


    It would seem that an instance of an object class would have to be an object so the is_object() test seems unnecessary.

    Does this code work in PHP4?

    Is it a speed thing? I imagine that most of the time the object passes the test so you'd have a net loss in speed by using both tests.



  • opengeek Reply #2, 2 years, 1 month ago

    Reply
    Quote from: BobRay at Mar 31, 2010, 12:46 AM
    I've been trying hard, but I can't see why this common code in MODx uses both tests:

    if (is_object($object) && $object instanceof xPDOObject) {


    It would seem that an instance of an object class would have to be an object so the is_object() test seems unnecessary.

    Does this code work in PHP4?

    Is it a speed thing? I imagine that most of the time the object passes the test so you'd have a net loss in speed by using both tests.

    No, it's an error thing, you can't check a non-object with instanceof or it throws a PHP error; both checks are necessary. The first says, is this in object, the next says is it an instance of the class I want.


  • BobRay Reply #3, 2 years, 1 month ago

    Reply
    Thanks. I knew I was missing something.

    I didn't know that the short-circuiting was guaranteed in PHP and was confused by all the examples like this that came up:

    if ($object instanceof xPDOObject) {


    On closer inspection, I see that they're all preceded by an is_object() test.