We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    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.

    huh
      Did I help you? Buy me a beer
      Get my Book: MODX:The Official Guide
      MODX info for everyone: http://bobsguides.com/modx.html
      My MODX Extras
      Bob's Guides is now hosted at A2 MODX Hosting
    • Quote from: BobRay at Mar 31, 2010, 05: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.

      huh
      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.
        • 3749
        • 24,544 Posts
        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.
          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
        • What if you know the object does exist or the function would never be called? For example, in a plugin using OnLoadWebDocument? You know that $modx->resource will necessarily exist. All you want to do is check if it's a modStaticResource, for example.
              
          case 'OnLoadWebDocument':
              if ($modx->resource instanceof modStaticResource){
                 ...
              }
          ...
          
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 3749
            • 24,544 Posts
            Sanity tests are always at the discretion of the coder. wink

            The is_object() test only takes a few milliseconds (possibly microseconds), though. Your case seems safe enough, but I've been bitten when somebody else (e.g., Setup) invoked an event I had a plugin tied to and the standard objects ($modx->user or $modx->resource) didn't exist.

            That can happen pretty easily with scripts that run outside of MODX, instantiate it, then call MODX processors.
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting