PHP Techniques

HateEternal

Mac Metal Head
I have in a way become frustrated with PHP. I like it, it's cool, but I have a lot of problems with the way objects work as well as the major differences between versions (but i guess that is true for any language).

The biggest problem I am having is conceptualizing the best way to do things. I have this compulsion to code using objects... but I almost never can find a way to fit objects into my code. All the pages i am doing are fairly simple, simply generating links and info from a MySQL database but I HATE how the code looks. I am so used to having really neat, well organized Java classes and this just feels so weird.

Well, I guess the point that was buried some where in that rant was that I want to learn better techniques for coding PHP, does anyone know any sites that have sample code that I could look at? I just need to get a feel of what the best way of doing things is.
 
It is interesting to see how my PHP coding has changed over the years, yet sometimes when I am in a hurry it reflects more of my early years of programming, basically what I learned off of, which really wasn't a good example, but functional non-the-less.

Although I consider myself a confident PHP programmer (give me anything and I can probably do it) my style is probably not that ideal and sometimes I look at code (such as this forum software) and shake my head thinking... maybe I am so yesterday on my approach. Obviously, I would be the first to confess that there is probably a better way than my way... but what I have learned is that there are so many different ways and I think that is why PHP is so attractive. No matter your style, you can still have the same output.
 
I use KISS - Keep It Simple and Stupid.

Sure you can use fancy classes, etc, but in the end most web sites don't do such complex things anyway.
 
I agree with ScottW. One of the things I liked so much about working in Perl is that everyone seemed to wholeheartedly agree that there was no 'correct' way to do anything (which means reading someone else's code is like reading poetry -- sometimes you just don't get it until you get it). In other words, the 'correctness' of code took a back seat to personal style and simple efficiency. Given its roots, I'm a little sad that PHP didn't pick up some of that philosophy. But then again, it was originally a pretty terse and limited language, so what would you expect?

Personally, I tend not to write any object-oriented code if I can possibly avoid it. Most code for me seems to be most natural when it is procedural. When I have tried to go 100% object-oriented, I always end up with procedural code wrapped in an object, which I think defeats the spirit of OOP. Sometimes, though, you can't avoid using object-oriented code; for example, anything with unlimited recursion or nesting just comes out so much cleaner when you write it as an object (XML parsers and message-boards are some examples).

I'd say just do what feels comfortable, don't be too dogmatic and find your own groove. If you are really interested, though, attach some timers to your code and track your memory usage. For me, efficiency is the ultimate judge.
 
I am more worried about the readablity of my code than the efficiency, the pages are short and really are just a few if statements and while loops.

Also, has anyone seen a technique to keep a MySQL connection live from page to page? It seems like it has to be so costly to connect every time you go to a different page(especially when your MySQL server is on a different server than your web server)
 
I'm with you on the readability issue. I build most of my pages assuming someone will later extend or modify them, so having a simple structure is pretty important. BUT... most of the people who do PHP coding tend to be self-trained and if you write code that someone else may have to maintain, you are pretty sure to be answering a lot of questions if you go object oriented. But that's my opinion.

As for MySQL... I am not 100% sure on this, but I am pretty darn sure that any recent build of Apache pools its database connections so that the connections are actually reused automatically, regardless of what the code literally says. This happens whether or not the database and web servers are on the same physical hardware. If the connections aren't used within a certain period of time, the connections are dropped and the memory freed. I'm less sure about IIS, but since it is ODBC I'd be really surprised if there wasn't some serious pooling going on there, too. You might move this to a top-level post or try a PHP, MySQL or Apache forum to get a more informed answer.
 
PEAR has a lot of classes ready made for all sorts of things, one of them being handling db connections and extracting and displaying data in (almost) any form possible.

As for reusing connections to the database; if you use mysql_pconnect to connect to older MySQL versions, or mysqli_pconnect to 4.1 or later versions, you will get a persistent connection (that's what the p stands for). It will be reused, instead of rebirthed, in later pages. There is a time out of course, and if I remember correctly, it can be set to your liking.

Hope this helps you.

PS: you might want to check out the "PHP Anthology", vols I & II from sitepoint (www.sitepoint.com). You can download a bunch of free chapters before you buy, so you can get a feel for the content. I am not affiliated with them in anyway, I just like their stuff...
 
Back
Top