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 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 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
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.
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
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
(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.
(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
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
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.
Bookmarks