Suggestion for Newbie PHP Developers

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
volomike
Forum Regular
Posts: 633
Joined: Wed Jan 16, 2008 9:04 am
Location: Myrtle Beach, South Carolina, USA

Suggestion for Newbie PHP Developers

Post by volomike »

I see a lot of screwy code as a freelancer. I'd like to suggest that more PHP developers either consider using a framework such as Smarty, Zend Framework, or something else. And for those who don't want to use a framework and even for those who do use a framework, at least consider what I call a logic template. A logic template is a simple, consistent way of commenting your pages and doing things in an orderly fashion. It makes it easier for the next developer to read what you've done.

I've come up with a logic template that I use over and over again, and it's a snap to start using. Here's the link:

http://volosystems.com/article.php?a=Co ... +Templates

It doesn't take more than a couple seconds to see this template and go, "I get it," and start using it, will be a small improvement for how fast you get work done, and will mean so much to other developers on teams you work with, or who have to look at your source code in the future.
User avatar
volomike
Forum Regular
Posts: 633
Joined: Wed Jan 16, 2008 9:04 am
Location: Myrtle Beach, South Carolina, USA

Re: Suggestion for Newbie PHP Developers

Post by volomike »

Another thing I see out there as a freelancer is either absolutely no variable naming convention on an inherited project, or some affinity to Java with its funky namingConvention. I wrote an article on this, criticizing the funkyMixedCase namingConvention of Java, and go back a bit through the history of naming conventions to discuss why I use the one I use, which is not Java-like.

http://volosystems.com/article.php?a=My ... Convention

Perhaps it can help you write cleaner PHP code.
Scrumpy.Gums
Forum Commoner
Posts: 71
Joined: Thu Aug 30, 2007 2:57 pm
Location: Bristol, UK

Re: Suggestion for Newbie PHP Developers

Post by Scrumpy.Gums »

Conventions are always a controversial subject. I'm not sure you can claim that one convention is always better than any other. Personally, I've never liked forms of Hungarian notation, where as others seem to love it. The most important thing is that the code is clear and concise.

With regards to the commenting template, something like that may help new programmers to get used to commenting. However, I feel that commenting is more of an art form and should be entirely dependent on the code. Again, this is subjective, some people may want to write a comment and then write the code while others want to write the code and explain in the comment why it's been done that way.

My two cents ;)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Suggestion for Newbie PHP Developers

Post by onion2k »

Those articles are awful. The first one tries to push a limited code template that won't work at all in a vast array of circumstances, and the second is a throwback to old coding notation that's inflexible and ultimately worthless.

Mind you, neither of them as as bad as your "don't use sessions!" article.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Suggestion for Newbie PHP Developers

Post by Zoxive »

onion2k wrote:Those articles are awful. The first one tries to push a limited code template that won't work at all in a vast array of circumstances, and the second is a throwback to old coding notation that's inflexible and ultimately worthless.

Mind you, neither of them as as bad as your "don't use sessions!" article.
I thought the same thing when I came across this blog the other day. (I thought it was going to be a joke at first, but I guess I was wrong.)
User avatar
volomike
Forum Regular
Posts: 633
Joined: Wed Jan 16, 2008 9:04 am
Location: Myrtle Beach, South Carolina, USA

Re: Suggestion for Newbie PHP Developers

Post by volomike »

onion2k wrote:Those articles are awful. The first one tries to push a limited code template that won't work at all in a vast array of circumstances, and the second is a throwback to old coding notation that's inflexible and ultimately worthless.

Mind you, neither of them as as bad as your "don't use sessions!" article.
I guess I want to ask you:

- I use that logic template for my business logic pages (the pages that call the toolbox classes and then interact with Smarty to draw the content on the screen). It hasn't failed me so far. I may use some sections, or disregard others. So far it's worked. So I guess I was interested why you think it won't work in the "vast array of circumstances".

- On the coding notation, I would be curious if you could provide a hyperlink to your preferred version, and then second, why is "old" a bad thing in all cases? Perhaps an old thing can also be a good thing, if carefully reconsidered.

- As for the sessions article -- plain and simple, session management with the $_SESSION object is a very bad idea because it is server specific and won't permit your app to scale in a web farm where, with every click, a user may return to a different server that does not share an existing shared memory state. So why is that a very "bad" article to state that?
User avatar
volomike
Forum Regular
Posts: 633
Joined: Wed Jan 16, 2008 9:04 am
Location: Myrtle Beach, South Carolina, USA

Re: Suggestion for Newbie PHP Developers

Post by volomike »

Scrumpy.Gums wrote:Conventions are always a controversial subject. I'm not sure you can claim that one convention is always better than any other. Personally, I've never liked forms of Hungarian notation, where as others seem to love it. The most important thing is that the code is clear and concise.

With regards to the commenting template, something like that may help new programmers to get used to commenting. However, I feel that commenting is more of an art form and should be entirely dependent on the code. Again, this is subjective, some people may want to write a comment and then write the code while others want to write the code and explain in the comment why it's been done that way.

My two cents ;)
I liked your thoughtful comments and how you explained what you meant. Yeah, I just wanted to get something off my chest in my way of seeing things. It's not the only view. There are many notations out there and one can choose what they want.

As for the commenting, I start with the commented logic template, fill in some sections, perhaps not others, depending on what I'm doing, and then add further comments as I clearly see that a future programmer (or myself) may get confused if they see a piece of somewhat complex code.
User avatar
volomike
Forum Regular
Posts: 633
Joined: Wed Jan 16, 2008 9:04 am
Location: Myrtle Beach, South Carolina, USA

Re: Suggestion for Newbie PHP Developers

Post by volomike »

Zoxive wrote:I thought the same thing when I came across this blog the other day. (I thought it was going to be a joke at first, but I guess I was wrong.)
A bit vague there, don't you think?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Suggestion for Newbie PHP Developers

Post by John Cartwright »

onion2k wrote:Those articles are awful. The first one tries to push a limited code template that won't work at all in a vast array of circumstances, and the second is a throwback to old coding notation that's inflexible and ultimately worthless.

Mind you, neither of them as as bad as your "don't use sessions!" article.
I agree. I actually just read a couple of the articles and I can say I 100% disagree with them.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Suggestion for Newbie PHP Developers

Post by onion2k »

volomike wrote:I guess I want to ask you:

- I use that logic template for my business logic pages (the pages that call the toolbox classes and then interact with Smarty to draw the content on the screen). It hasn't failed me so far. I may use some sections, or disregard others. So far it's worked. So I guess I was interested why you think it won't work in the "vast array of circumstances".
Well.. for the code I write your template would be wrong for several reasons:

1. You authenticate the user prior to initialising any variables, and fetching get or post data or from cookies. How are you authenticating the user exactly?
2. You read things from the database prior to any updates - what if you have triggers that update tables you need to read from? I always update the data first, then read anything needed for the page.
3. You have 'biz logic' sections in the body of a script. I believe business logic should be encapsulated in code in either objects or functions - it's likely to be reused elsewhere in the site.
volomike wrote:- On the coding notation, I would be curious if you could provide a hyperlink to your preferred version, and then second, why is "old" a bad thing in all cases? Perhaps an old thing can also be a good thing, if carefully reconsidered.
I didn't say all old things are worthless, I said that an old, inflexible coding style that's basically a lazy form of Hungarian notation is worthless. HN in a loosely typed language like PHP is silly in my opinion. Your variable name will be wrong if you access the variable in a certain way - eg you may have called something $iCounter but if you use it in "echo $iCounter;" technically it's a string.
volomike wrote:- As for the sessions article -- plain and simple, session management with the $_SESSION object is a very bad idea because it is server specific and won't permit your app to scale in a web farm where, with every click, a user may return to a different server that does not share an existing shared memory state. So why is that a very "bad" article to state that?
That's wrong on so many levels. Firstly you're ignoring the fact that you can use a persistent load balancer to always send the user to the same node, so $_SESSION will work fine. Second, you mention in the article that storing session data in a database is a bad idea because it'll fill up very quickly - what's wrong with having a job that runs occasionally that clears out old data, in exactly the same way that PHP's session handler clears out old session files? And thirdly, and most importantly by far, 99% of PHP websites will never grow to the size that they'll need more than one server. PHP's standard $_SESSION handler is a great solution for storing statefull data on small websites. Why would you discourage people from using it just because there's a tiny chance that a long way down the line their site might outgrow it?

EDIT: Fourthly, you recommend storing data encrypted and compressed in a cookie. What happens if users don't have cookies switched on? Sessions will continue to work via PHP adding the session id to the GET variables. Your idea will fail completely.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Suggestion for Newbie PHP Developers

Post by Chris Corbyn »

As far as naming conventions go I'm not bothered what convention people use provided it's consistent. In response to "ProperCase" vs "mixedCase" the reason Java uses "mixedCase" is to distinguish between ClassNames, fieldNames, CONSTANT_NAMES and methodNames() quickly.

Smarty isn't a framework by the way ;)

I don't think I need to comment on your other articles since I'd only be re-iterating what's already been said.

It's great that you're voicing your opinions... I'm not sure you're doing your business any favours by publishing that content however ;) It seems very ill-informed from where I'm sitting.

PS: I manage just fine with MySQL replication + PHP's session handling features when I'm running an app across multiple servers.
User avatar
volomike
Forum Regular
Posts: 633
Joined: Wed Jan 16, 2008 9:04 am
Location: Myrtle Beach, South Carolina, USA

Re: Suggestion for Newbie PHP Developers

Post by volomike »

Guys:

There, that's better. That's what I needed to hear. I can deal with this more than "your site sucks".

Some answers/comments to your questions/statements:

- I say init vars after auth because yeah, I do init some vars before auth. It's just that I mean that I need to get the user the heck off my page if they aren't auth'd properly, and I usually do it before I start filling up a bunch of vars with initial data because there's no sense in doing that when the guy isn't auth'd properly and doesn't belong there.

- reads before updates. It's just that I normally have to do reads before updates. Not that this is always best, but it's just that I normally do that. So, yeah, if I have to do an update before a read, then so be it and I comment the exception.

- biz logic. My definition was a little loose there and I appreciate your opinion because I think I'm going to change my logic templates somewhat. I guess I should reword that to "glue code". I mean, in those sections, I'm calling either my local functions or my instantiated objects, and the glue code is the logic of conditionals, assigning variables, iterations, etc. to orchestrate what the script is going to do like the conductor of an orchestra.

- You are right that PHP is duck-typed and therefore HN might seem worthless. It's just that I think it's easier for developers further down the road, who see my code, to know that I really meant for the most part that this variable be used for strings or for numbers, etc. For instance, $nName and $sName seem to mean a little bit different things. It's just a visual, readability aid. Does that mean I might not use a string function on $nName? No -- I just might. It's just that for the most part, 80/20, I mean to imply that $nName is meant to be used for its numeric value.

- sessions. My experience with load balancers may have been a bit more limited than yours, and what I saw was that $_SESSION didn't work. I'll have to give this some more thought because I wasn't aware of persistent load balancers. As for cookies switched off, if users did that, then they pretty much couldn't use 99% of the web, so I know that users won't do that because most sites wouldn't work for them. As for session data in the database, I do use it when it makes sense, but not if it doesn't make sense and might fill it up with frivolous calls for users who might only click around a couple times and leave.

- Smarty. Okay, okay, I get it. Not a framework. Check.

- naming conventions. I guess it comes down to taste. I just found createAccountingUser() seemed slightly weird to me compared with CreateAccountingUser(). Sure, I can see where it might make sense, it's just the taste thing I have about keeping it "old school". At least I'm not going old, old school, where my variables would be in COBOL-type like: FIRSTNAME.

- site. Yeah, I think I'm going to revamp the site a good bit. I've been asking opinions from all over the place and getting very good feedback that I'm listening to. Most of my customers are affiliate marketers and web designers who don't really care about these articles in the first place. I thought I would get a little bit of ad revenue as well, but that's been about nil. So I need to revamp the site to cater more towards their interest. Some like blogs on the home page -- a couple of my mentors on the web use this technique -- while others do not and recommend starting with the portfolio. It's a little hard for me to do that, though, since I'm trying to build up a portfolio. So far I've had two NDAs which didn't permit adding their site to my portfolio (and because they were intranet things), and one client who was trying to lure more clients of his own in and wanted to say the site design and web development was entirely his. (I don't complain, since he's been a big help to me in giving me work, suggestions, and referrals.)
Post Reply