Friday, September 16, 2011

PHP Programming for Beginners

Sometime soon, I think someone is going to ask me for some tips about breaking into programming. This person has an engineering background, but that's about all I know for the moment. So this post is a bit of a pre-emptive answer on things that I think someone looking to get into programming should take a look at, as a part of a self training exercise.

But first, a bit of preamble. In late 2004, I moved back home to Australia from the UK. I'd been doing Progress Webspeed while I was over there, and had done a small project using PHP in my final month. When I got back, I was determined to change programming languages. I wanted to do Java. I'd been wanting to do Java since about 1998 when I started my first web development project, an internal contact management system. Whilst my specs for functionality in the project were approved, my choice of programming language was not. I had to use Progress Webspeed, and it was very early days for that product.

Fast forward to 2004, and it was time to have another crack at breaking into Java. By this time, I had nine years of software development experience under my belt, and a better understanding of the web. I just needed to work out how to fit Java into that, in a practical way. A good friend pointed out a few technologies to learn. Spring Framework with its Inversion of Control, Hibernate for database abstraction and JSP for the web pages. So I spend a few months learning those things, and managed to land a contract doing Java with those technologies. On the job, I also learned a bit more about Spring, a practical, but narrow application of MVC and the SunONE server, and how to deliver Spring and JSP functionality in Portlets.

Somewhere along the line, as a part of the contract, I delivered a content management system in a shared hosted environment. That was done using Joomla!, and a very nasty but useful arrangement of soft links to allow upgrades across 240 sites at once, across 3 web servers. And with this, I started to learn about PHP again.

In late 2007, I left that contract and started at my current job, doing PHP and web development as a full time role. I remember fondly of those days spent programming in Java. Strict types, IDE assisted refactoring, IoC and MVC. It seemed to be a golden age. Then annotations started to take hold in Java, for Spring, for Hibernate, for code injection. And I was thrust into a world of legacy PHP code, and flat file structures. It seemed like a return to the dark ages, and all that great Java knowledge was going out the window. But the lessons are the same, and I hope to impart a few of those to anyone willing to read.

This tech, I do impart.

Since it's PHP I'm doing these days, I'm going to assume that's what you're interested in too. And since I'm doing web, well, I'm going to focus on that as well. And if you're interested in neither of those things, stick around, because there are some things any any programmer should be doing. I might even knock those off first, just so you don't have to read to the end. It's a warning. If you haven't checked your scroll bar yet, this post could get lengthy. About as long as my attention span.

So, before we even start with the actual programming stuff, I recommend you look at source control first. Source control is how you record changes to your code, and share your code with others in a team, or even as third party contributions. I strongly recommend Git. It's light, it's free, it's available for all three platforms, use it. It's a distributed source control tool, so you don't need to be online with a server (like SVN) to commit changes.

Next off, document! Document your stuff. Have a docs directory in the root of every project you do, and put stuff in it. Put plain text documents in it. They make it much easier for source control. Maybe use a markdown format, maybe use a LaTeX format (nice for generating PDF manuals later on). Just make sure you document.

Learn MVC. That's Model-View-Controller. Even if you haven't picked up programming language to use yet, most modern programming languages that you are likely to use are going to be able to support this coding arrangement. Done well, it will help separate programming concerns, and make life easier for you. Done badly, as long as it's still MVC, someone else might be able to refactor the code and make it look nice. If you're playing in the Java world, you may want to investigate IoC as well. That's Inversion of Control. Good for configuration based frameworks.

Learn a Framework. You might start looking at the MVC stuff, and baulk at the thought of having to implement a dispatcher to make use of all that MVC stuff. Chances are, someone else has done it for you. So pick a framework that implements MVC. For Ruby, I recommend Ruby on Rails (is there anything else?) and for PHP, I recommend CakePHP (there are plenty of others). For PHP, you could also go with Symphony, Kohana, CodeIgniter, Yii or Lithium, just to name a few. Some are PHP 5.3 only, some have hang ups from PHP 4.2, and haven't fully gone OO with all of their code. For Java, my knowledge isn't really current, so I'll recommend Spring Framework, if it's still kicking around. I remember something called Seam (from jBoss, I seem to recall). Perhaps look that up too. If you've gone down the Microsoft path, I guess .NET is going to be your bag. But even that is not going to force you to use MVC.

Learn a development methodology. Even if it's just the basics of requirements, design, development, development testing, user testing, documentation and delivery. There's agile programming, rapid application development, test driven programming, the general umbrella of extreme programming, and the old "reliable" waterfall methodology.

Use your online resources. Google has always been your friend, but I'm finding StackOverflow to be even better for specific programming questions, and awesome answers.

And I think that's about it for the programming agnostic stuff. For those that read on, we're diving into the shallow end of PHP web development. My attention span is waning, and it's 12:20am.

So, so far you've got Git for source control. Next, you'll want an integrated development environment (IDE) to do your development in. Something with syntax highlighting, something with a little integration for source control. Some people get away with Textpad, or Textmate. I'm going to recommend Eclipse with PHP Development Tools (PDT). These also Aptana, which is based on Eclipse, which works well too, and takes care of the Git integration, using EGit, but you can install EGit separately if you want to. I tend to use msysgit, and mix it up between the GUI and the bash shell.

Next, you'll want a PHP version. In fact, you'll want a whole web server, PHP, MySQL stack. Depending on your platform, there's LAMP (for linux), XAMPP (from Apache Friends on Windows), WAMP (also on Windows) and MAMP (I think that's what it is called, and it's for Macs). I used to run XAMPP, then tried WAMP. These days, I'm running a hack version of WAMP configured to run two Apache instances at the same time, to target PHP 5.2.17 in one, and PHP 5.3.6 in the other. (Oh, look at that, it's up to 5.3.8).

Officially, PHP 5.2 is no longer supported, but many web hosting companies are still running it. I'm not sure what the take up of new languages features for PHP 5.3 in the frameworks. I know Lithium only does PHP 5.3, using namespaces quite heavily. CakePHP doesn't use any PHP 5.3 specific features yet. CakePHP 2.0 (RC1 at the time of writing) supports 5.2 and 5.3. I'd recommend know where you are deploying to, first, before making a decision of which PHP version to go with. However, given a choice, definitely go with PHP 5.3. All the online doco assumes PHP 5.3. The online doco, BTW, is one of the winning features of PHP. Sure, function calls don't have a consistent naming strategy, but at least there's some sort of documentation for most functions, with somewhat helpful, user contributed comments.

You may want to learn a bit of SQL. Specifically, you'll probably be using MySQL or SQLite. The cool kids are playing with simple, document driven databases like MongoDB or CouchDB. Start with a relational database management system (RDMS) first, and learn about database design, normalization, primary keys, indexing and redundancy for performance.

Then start looking at your web technologies. At a high level, it's HTML, CSS and Javascript. Specifically, it's HTML5, CSS3 and Javascript. Anyone running a browser that doesn't support HTML5 needs to get a new browser. Web standards won't move forward, if we're all living in the past.

For HTML5 editing, just use the text editor. Actually, first, read Dive Into HTML5. Then use the text editor. You may have heard about Frontpage. It no longer exists. There's a reason for that. Don't use GUI HTML builder thingies, or zombie jebus will kill you in your sleep.

For CSS3, you'll be using a text editor again. But you might like to spice it up with either SASS or Compass. Since Compass uses SASS, that might be a nice place to start.

For Javascript, again with the text editor. However, a framework is nice. I'm going to recommend jQuery. It's awesome. There's also MooTools, DoJo, Prototype and script.aculo.us. For JS based UI stuff, I tend to mix it up. Usually, I use a bit of jQuery UI, but it really needs a lot of work. ExtJS from Sencha looks really good, but it's not free for commercial use, and the dev team can be a bit abrupt with the newbies.

Then get a modern browser with decent debugging tools. IE9 has finally caught up to Firefox and Chrome, but I'd start with the latter two first. These days, I use Chrome for all my development, but you might like Firefox, and particularly the Firebug and WebDeveloper plugins.

It's 1am and I'm fading fast. What have I forgotten?

Oh yeah, the PHP Framework. Use CakePHP! To see a pretty good application written in CakePHP, check out Croogo. It's a CakePHP based CMS that you can extend any way you want to.

Okay, I've got to head to bed, and I've got a feeling that I've only scratched the surface. If there are items you'd like me to expand upon, leave a comment, and I'll do some follow up posts, looking at things in a little more detail.

2 comments:

Great info! I recently came across your blog and have been reading along. I thought I would leave my first comment. I don’t know what to say except that I have enjoyed reading about programming language. PHP Programming Company India