I spent more time than I should have today debugging an issue (that turned out not to be a bug, just user confusion), due to my confusion resulting from some class inheritance behavior. Simplified, the situation was:

class A extends OurFrameworksControllerLikeClass
{
    public function x()
    {
        // do some stuff, then...
        $this->getDataservice()->foo();
        // do some more stuff
        return $something;
    }

// bunch of other methods
}

class B extends A { } // this is not simplified, that's all it did: extended "A"

class C extends B
{
    public function x()
    {
        // do some stuff, then...
        $result = parent::x();
        // ...do some stuff based on $result
    }
}

Where I got confused was that each controller-type class can have a corresponding (singleton) model-like class that is automatically invoked by that getDataservice() method. So when I looked at the line in C::x() that called the parent::x() (from class 😎 and saw that class B did nothing but extend class A, I was then reading through A::x(). When it called $this->getDataservice->foo(), I was looking through class A's dataservice to figure out what it was trying to get from the DB, when all along I should have been looking at class B's dataservice. After probably 40-50 minutes of wondering why none of my die()'s in A's dataservice were executing, it finally dawned on me what I was doing wrong. At that point I found the query being executed in class B's dataservice that showed me where the issue was, and all was well (if maybe an hour later than it should have been).

Possibly that's all self-evident to some of you, but I figured it might be worth a giggle at my expense, at least, and might provide a cautionary tale to others. 🙂

    8 days later

    The clarity that OOP ostensibly brings to one's code by allowing you to group your variables and functions into classes is often more than compensated for by the confusion about where you are inheriting from.

      In my continuing story of PHP hilarity...

      Today I couldn't figure out why one if() condition was not firing. So I stuck in some error_log() statements at various points in the methods involved to find out what was wrong. I had them output their debug info with leading dashes so I could see the nesting level at a glance, something like:

      foreach($something as $thing) {
      error_log("--got here");
      // later...
      error_log("----$count");
      // later...
      if($count > 0) {
        error_log("------in the if");
        // do stuff that wasn't getting done for some reason
      }
      

      Debug output looked like

      --got here
      -----4
      --got here
      -----6
      --got here
      -----5
      

      Couldn't figure out why the if() condition wasn't firing, until I changed the middle error_log() to:

      error_log("****$count");
      

      ...at which point the output was:

      --got here
      ****-4
      --got here
      ****-6
      --got here
      ****-5
      

      ...and realized I hadn't noticed that 5th "-". :rolleyes:

      (Root cause of the problem was that I had set $count as $x - $y, when it should have been $y - $x.)

      PS: Shout-out to Rulian for helping me see my silly mistake, even if the reason he thought the dashes were the problem was not the actual reason, but he got me to change them to "*" in any case. 🙂

        NogDog;11019377 wrote:

        So I stuck in some error_log() statements at various points in the methods involved to find out what was wrong.[/QUOTE]Wouldn't it have been easier to set a breakpoint in your IDE, add variables/conditions to your watch window, and step through the code? :p

          Nah, 'cause then I'd have to learn a different IDE (get company to pay for it? :eek: ) and learn how to use the debugger, and that would take time! 🆒

          (Besides, it looks cool to pointy-haired bosses when they see my terminal window with "tail -f php-errors.log" scrolling a bunch of arcane-looking text while I'm debugging. 🆒 )

            NogDog;11019389 wrote:

            (Besides, it looks cool to pointy-haired bosses when they see my terminal window with "tail -f php-errors.log" scrolling a bunch of arcane-looking text while I'm debugging. 🆒 )

            Trust me, they're equally amazed when they watch you tap a couple of keys and jump all throughout the source code whilst you hover your mouse over bits and pieces. :p

              NogDog;11019389 wrote:

              (Besides, it looks cool to pointy-haired bosses when they see my terminal window with "tail -f php-errors.log" scrolling a bunch of arcane-looking text while I'm debugging. 🆒 )

              <3 Tail!

              (Umm, that's tail(1) ... what were you thinking I meant?)

                dalecosp;11019425 wrote:

                what were you thinking I meant?)

                $ man woman
                No manual entry for woman

                🙂

                (I happened upon this page once upon a time and nearly cried because I was laughing so hard.)

                  I think I've posted that one here before.

                  Seems like (?) that only worked on BSD once upon a time; but it's certainly true on CentOS at present.

                    NogDog, I typically rely on logging to tell me what is happening in my code, but have been mightily impressed with what is possible with a good IDE and proper debugger. With all the crazy frameworks and their crazy inheritance schemes, it can be a real blessing to be able to step through the code rather than busting one's cranium trying to follow the code in one's head. The first time I was able to properly step through code, I felt like I had come out of the stone age.

                    At the same time, I have found debugging in Eclipse to be enormously frustrating. I've spent hours setting up Zend Debugger and XDebug, only to have them break when I do a software update. Eclipse PDT is really finicky IMHO and the debugging stuff is flaky and unstable.

                    I've heard good things about Komodo and thought this page looked really informative:
                    http://docs.activestate.com/komodo/4.4/debugphp.html

                    It's not like snapping your fingers, but it can be quite a revelation. I'm hoping to find a debugging setup that is stable and reliable.

                      Write a Reply...