3. Development tools
So far we’ve outline some of the basics of using PHP without a web server, but before we really get into the nitty-gritty details of developing useful non-web software we’re going to take a slight detour into the world of PHP development tools. This is for two reasons. Firstly, before deciding which techniques and methods you are going to use (Interactive CLI script or GUI interface) it is important to understand what tools are available to help support your choice of programming methodology. Secondly, you will often need to adopt a different workflow when developing a new type of software that you may not be familiar with, so you may need to find new tools to fit your new way of working.
In general, you can use the same development tools for general purpose programming as you do for web development, after all PHP syntax is the same wherever it’s executed. The main differentiator is your workflow, and you may find yourself being more productive with different tools.
Of course one of PHP’s dirty secrets is that many PHP programmers don’t use debuggers, unit testing, build systems or code profilers, and in many cases don’t even know what they are. Usually it’s because many PHP scripts are developed as small, uncomplicated, low-risk projects by single developers, and the overhead of learning and deploying extra tools is an unnecessary burden (particularly for those without a formal computer science or programming education). Don’t worry if you fall into this camp, we’ve all been there (and like to visit from time to time!).
However when you start venturing into general purpose programming, you’ll find that the complexity of projects often increases. Rather than a selection of short lived scripts with limited shared state executing in less than a second, you’ll get into larger code bases with longer running processes and more complex dependencies. In such scenarios, the effects of bugs can be magnified and tracking them down becomes increasingly hard. Code is revamped less often and can be relied upon by an organisation and remain deployed for much longer, increasing the burden of maintaining code in the future. If you’re not a regular user of the type of tools described in this chapter, you might find this the ideal opportunity to delve into the wider world of PHP development tools, and give yourself a good foundation on which to start building real software.
We’ll look in turn at different classes of tool, and give some examples of, and links to, popular tools of each type.
3.1 PHP REPLs
When you want to test out a few lines of PHP, your default instinct may be to create a new PHP file, save it, and then execute it with PHP. There is a better, faster and more interactive, way however. The PHP “Interactive Shell”, also known as the PHP REPL (Read-Eval-Print-Loop), is a quick and easy way to type in code and have it execute immediately. Unlike executing single lines of code using php -r, the REPL (started by calling php -a) keeps the scripts state (e.g. contents of variables and objects) in-between each line that you type, until you exit. You can use all of PHP’s functions, although no libraries are loaded by default, and you can include() or require() existing files of PHP code. This latter capability is useful for debugging the final output of a problematic script; simply include() your script which will execute the script and, as long the script doesn’t terminate prematurely, then you can echo() or print_r() or otherwise explore the state of the variables and other resources at the end of the run. The following example is a capture of an actual interactive REPL session using the standard PHP REPL. Other brands of REPL are available, and are listed later in this section.
3.2 Build systems
Build systems are used for building and deploying software to development and/or live systems. They automate many of the repetitive tasks that occur when deploying a new revision of a software system. Build scripts and build systems originated with languages like C which required compilation before the software was able to run, and covered steps such as compiling and linking various software modules as well as managing source code dependency issues. Although PHP is not a compiled language, a build system can still be a great time saver performing tasks like :
- gathering and managing assets (images, data files etc.)
- packaging or moving scripts and assets
- archiving older versions or committing to source control systems
- running automated tests, static analysis and other quality control processes
- running code minification or obfuscation processes
- starting and stopping related services
- refreshing and resetting your test environment
- cleaning or initialising databases
- documentation generation
- team notification
or indeed anything else needed to ensure the latest version of your script is deployed successfully. As well as saving time, it also helps to ensure consistency and prevent mistakes. As you’ll no doubt know, trying to fix an obscure bug that occurred because you forgot to copy over the latest copy of a minor data file isn’t fun.
You can of course construct your own build system using shell scripting (or even better, PHP CLI scripts!), which may suffice for small or simple projects. For larger or more complex systems an off-the-shelf build system may be a time saver. Many such systems can cope with PHP, but one of the best for PHP work is Phing. Phing was built for PHP, and can be easily extended with PHP classes and has a wide range of PHP related tasks already built in. Telling Phing what to do is a matter of creating simple XML files, which you can even create programmatically using the PHP XMLwriter extension if you need to.
3.3 Continuous Integration
3.4 Debuggers
A debugger provides an easy way for you to inspect the internal state of your application, often while it runs, at fixed points in the code, or after a crash. This can give you valuable insight into bugs, particularly those in long running code or those relating to how external data affects variables, as well as direct flaws in your code. Debuggers can give you a “stack trace”, which shows the nested set of functions (or “stack”) you are currently in. They can also provide the current contents of variables, objects and their members, the state of resource identifiers and so on. Some additionally provide profiling capabilities (see the section on profilers later in this chapter).
There are a number of debuggers available for PHP, the most prolific (and arguably the most versatile) is Xdebug.