Just thought I'd share my inheritance confusion
Results 1 to 10 of 10

Thread: Just thought I'd share my inheritance confusion

  1. #1
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    13,886

    Just thought I'd share my inheritance confusion

    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:
    PHP Code:
    class extends OurFrameworksControllerLikeClass
    {
        public function 
    x()
        {
            
    // do some stuff, then...
            
    $this->getDataservice()->foo();
            
    // do some more stuff
            
    return $something;
        }

        
    // bunch of other methods
    }

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

    class 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 B) 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.
    Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett

    "But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because he’s a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html


    eBookworm.us

  2. #2
    Senior Member
    Join Date
    Apr 2003
    Location
    Silver Lake
    Posts
    4,833
    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.
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

  3. #3
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    13,886
    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:
    PHP Code:
    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
    Code:
    --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:
    PHP Code:
    error_log("****$count"); 
    ...at which point the output was:
    Code:
    --got here
    ****-4
    --got here
    ****-6
    --got here
    ****-5
    ...and realized I hadn't noticed that 5th "-".

    (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.
    Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett

    "But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because he’s a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html


    eBookworm.us

  4. #4
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,399
    Quote Originally Posted by NogDog View Post
    So I stuck in some error_log() statements at various points in the methods involved to find out what was wrong.\
    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?
    Last edited by NogDog; 11-30-2012 at 01:40 AM.

  5. #5
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    13,886
    Nah, 'cause then I'd have to learn a different IDE (get company to pay for it? ) 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. )
    Last edited by NogDog; 11-30-2012 at 01:41 AM.
    Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett

    "But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because he’s a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html


    eBookworm.us

  6. #6
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,399
    Quote Originally Posted by NogDog View Post
    (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.

  7. #7
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,686
    Quote Originally Posted by NogDog View Post
    (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?)
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  8. #8
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,399
    Quote Originally Posted by dalecosp View Post
    what were you thinking I meant?)
    Code:
    $ 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.)

  9. #9
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,686
    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.
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  10. #10
    Senior Member
    Join Date
    Apr 2003
    Location
    Silver Lake
    Posts
    4,833
    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.
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •