Development environment - The PHP Project Guide (2014)

The PHP Project Guide (2014)

4. Development environment

Your development environment matters. Its where you’ll spend all or most of your time writing code, writing tests and fixing bugs. Your development environment needs to suit you and the needs of your project.

You also may need to get used to working within the same environment in different physical locations, so an environment that works on a variety of operating systems is helpful. Of course, I’m simply talking about a text editor, whether this is on the device you’re using or an online editor.

This chapter won’t be too long, cover too much or tell you what you need to use. After all, it’s personal.

4.1 Choosing a text editor

Texts editor come in many forms - from the lightweight to the bulky, meaning they’re more like an IDE (Integrated Development Environment). For basic projects or when you’re first starting out, it’s probably a good idea to start with a basic text editor to allow you to focus on the code. If you’re still learning PHP in depth and have a long way to go, it’s a good idea to use a text editor that doesn’t autocomplete your code for you. By this, I mean editors that finish a function name for you based on what you’re typing, usually pulled from a library that comes bundled with the editor. This makes it really fast to develop because you don’t need to go anywhere else to look these autocompletes up.

There are drawbacks to this though. * You might not absorb what you’re writing if it’s being autocompleted for you. * If something is deprecated you won’t know. It’s better to look it up in the manual.

Personally, I learn more when going and looking up information, as I often find myself lost in a tangent of information and jump from comment to comment or website to website finding better ways to do what I originally wanted to. This really helps when learning because you get different opinions and get a fuller picture.

Text editors can often be bulky and get in the way with what you’re doing. Remember, all you’re writing is a plain text file to be compiled by your server. Do you really need hundreds of fancy functions within your text editor to assist you along the way? What happens when you’re put into an environment when you’re writing code without this functionality and can’t expand and collapse code blocks and request all sorts of useful things to happen with the click of button? However unlikely it may seem that you’ll be pulled away from such an environment, it helps you become a better programmer when you don’t rely on a vast amount of tools.

4.2 Development envionment settings

Problems can arise with members of a team using different settings, such as line indentation when tabbing. You may find that one person has two spaces assigned to a tab where some have four. This means that when code is working on by more than one person and settings vary, you can end up with a mess, or, it’ll be really hard to modify files as you’ll have to keep manually changing tab spacing or character encoding.

The solution is to use something like EditorConfig. This nifty tool allows you to define specific standards like indentation type and size, as well as if line endings should be removed and the character encoding of the project files.

A typical EditorConfig file looks like this.

1 root = true

2

3 [*]

4 charset = utf-8

5 indent_style = space

6 indent_size = 4

7 trim_trailing_whitespace = true

This sets the character encoding to utf-8, sets the indent to 4 spaces and removes trailing whitespace on all lines. This file (named .editorconfig) would be transferred around with the project in the root directory and will apply these rules to all developers who have the EditorConfig plugin installed.

Horray for consistency!

4.3 Version control software

Version control software allows you to carefully manage changes made to your application, typically through ‘commits’ and then syncing with the main project. You can usually create ‘branches’ which give you a fresh copy of the codebase and let you do what you want with it, including the freedom to mess everything up if you desire. You can then merge the changes (if all is well) or simply discard the branch.

This has a massive advantage over any other method of development as it allows any changes to the main application to be made in isolation from the live code. The benefit of working like this is the obvious, changes don’t interfere with live functionality and often branches can be rolled back once pushed live, so if something goes wrong everything can simply be undone and the branch development can continue, either to fix the problem or remove it altogether.

The only fiddly part to developing this way is conflicting file changes. Conflicts occur when two or more developers have worked on the same file. Because changes per developer don’t immediately show for all developers, files need to be merged so the changes from each file are incorporated into one before they go live. This isn’t too much trouble if there are only a couple of changes, but sometimes a vast amount of changes can leave both developers confused about where code has been added, removed or modified. This can prove particularly complicated when both developers may forget which small changes they’ve made and are unsure which is the correct change. For example, changing a CSS width value from 10px to 11px. Luckily, there are tools that allow merges to be made and assist in the process by highlighting changed lines so the files can be easily merged. Typically, the two files sit side by side in a text editor environment and can be compared side by side.

Git

Git is version control software. It’s extremely fast, flexible and easy to use. It handles versioning extremely quickly. Git can be extremely easy to use, but offers powerful functionality that give you complete control and flexibility when working either by yourself or as part of a larger team.

Explaining the ins and outs of Git is way beyond the scope of this book, so go ahead and read up on it.

I’d highly recommend Pro Git by Scott Chacon. Don’t let the name put you off, it’ll slowly introduce you to Git as well as cover the more advanced use of it.

GitHub

GitHub is a popular resource for hosting code and implementing Git version control. With GitHub, you can create public (open source) or private repositories that allow to you host your application’s code and allow a larger team to contribute to.

The beauty of working with something like GitHub is social coding. If you have an idea for a project and want to host it for others to use and contribute to, chances are you’re going to end up with a much better codebase than you had before. By opening up your idea with the world, you also get more exposure and open source software is a generally a great way to go. Even if you’re making a profit from your idea, opening up the code is a very admirable thing to do and doesn’t nessasarily mean others will steal your idea.

As mentioned, if you want a private repository, that’s fine! It works in the same way, it’s just closed off from the outside world and only those invited as contributers to the project can work on it and see the code.