I have spent the last couple days of my life observing and occasionally lending a hand in some of the php channels on irc.freenode.net. While I was watching questions and answers fly by in #php I observed something peculiar that has now led me to this thought: One of the biggest problems many new PHP Programmers face is that they completely overthink their problem.
What I mean by this is that they have a tendancy, when they run into a new problem, to quickly think of a single possible solution, and then before coming up with a few alternatives they just dive headlong into that solution. After thinking about it a little longer I also realized that more experienced developers have the exact same problem. I’ll get to that in a second. First I want to talk about the events that led me to this conclusion.
For instance, someone asked the following question: Is it possible to suck jpeg data out of a database and display it in a webpage without saving the data to a temp file? Now, first off most of would probably say "why would you want to do that? It slows down your db! Save the image to a file, reference the filename somehow in your database and then create a script that will just pull the filename out of the database and pass thru the contents. That reply however would be completely countertuitive to my example and I don’t want to go dig through logs to find another one.
So, in my fantasy world where we are not going to tell him that he doesn’t want to store the image in a database we would tell him this: "Sure, just create a seperate script that will accept a record id, have it load the image field from that record and pass the results with an image/jpeg header.
Well…here is what happened. The guy asking the question is Q, the guy(s) answering are A<n> (n denoting the different answerers)
Q: Is it possible to suck jpeg data out of a database and display it in a webpage without saving the data to a temp file?
A: yes, store as a blob. then re-create the headers using php’s header()
A: I do this for dynamically generated pdf’s…but same for graphics
Q: thanks. I tried that, but I only get the image.. What I want is to embed this image in html. When I use the header method it doesn’t render the rest of the html.
A: like inline mime images?
Q: yea, I guess. You know, have some text, tables, and then this image that I sucked out of the database all on the same page.
A: so you want the image data embedded in a single html output?
Q: yes, if possible. I guess I could store the image data in a tmp file then use <img src> but I was hoping I could do it without using tmp files. do you know if this is possible?
A: I’m pretty sure it’s possible…with modern browsers. but pretty rare to inline images like that
At this point another guy jumps in to lend a hand….we will call him…A2
A2: you can’t mix text and image data in the same docuement, without using browser specific tricks
Q -> A2: ahh, okay
A2: your best bet is to have a sepeerate image "proxy" poage, that you simply call with parameters to return n image. like image.php?imageid=102
A: best to just use sessions and pass stuff to an image gen script
Q: Hmm. I can probably do <img src=’createImage.php’> and then output the headers and data in the createImage.php file.
At this point everybody tells him yup, that’s what you want to do, he then says thanks and accuses himself of having a ‘brain hiccup’
So, from what I gathered of the situation it almost looked as though he knew what the solution was…he just kept on subconciously blocking it out so he could figure out how to literally embedd the image within the page. At first I thought, rookie mistake. Then I realized I am guilty of the same thing sometimes. Granted I don’t fret over things like how to display images effectively. However, I probably have at least a few more years of experience under my belt. He’s probably just getting started, so it’s perfectly acceptable for him to have the question of how to display images the best. I tend to make my overthinking mistakes on a much more grandios scale.
One of the biggest problems we have when it comes to overthinking is we overthink how we can shoe horn the latest, hippest, new technology into our projects. When SOAP got big I actually contemplated creating a central e-commerce service for all my customers that all of their websites would use. So instead of having a seperate database and deployement for each client I would have a centralized database with a single domain layer deployed and made accessible via SOAP. Then I would recode all my client sites to use this SOAP interface. I even came up with phony excuses of why this would be a great idea for my clients. Things like, "I could set up a profile system so visitors would be able purchase something from one site and if they wanted to save their billing and shipping addresses they could and then they could load it when they visited any other client’s site." What I was ignoring was the fact that every single one of my clients at that time were small to medium local businesses that had pretty much nothing in common with each other. I very nearly scrapped working code for roughly 20 sites to create an ugly, ugly solution that wouldn’t do anything better and would probably do alot worse.
Now the newest thing is AJAX, and we are all again scrambling to figure out, "how can I use that in my code?" or more appropriatly for some of us "how can I use this in ALL of my code?" Now, don’t get me wrong, I like the whole AJAX concept (hate the name…even if it’s very…clean) and I like the whole SOAP concept. My point is that us, "bleeding-edge" developers have the tendancy to try and fit all this technology in alot of places where it probably doesn’t belong.
Now, to try my best and bring this full circle. If we didn’t concentrate so much on new technology maybe we wouldn’t have all these "pushing square pegs into round holes" problems. I am now resolving to give equal thought time to both new technology and the old ways of doing things. Like some wise man once said, "Moderation is the best medicine." New technology isn’t bad, but old, proven technology is pretty darn good.
So, am I crazy, justified, or just plain stupid?