I often see people having a hard time writing PHP (or any code for that matter), and I have this feeling they are doing it harder for themselves than what it really has to be.
[hr]
Have you ever gotten errors like these, and spend way too much time tracking them down and fixing them?
Parse error: syntax error, unexpected '}' in /srv/www/test2/public/test.php on line 171[hr]
Have you ever mistakingly done this:
[php]if ($userId = 1) {
// You think this will only run for user id 1, but it will run for everyone
showSuperSecretAdminControls();
}[/php]
Who am I kidding, we all have, it’s a common typo…
[hr]
Have you ever spent ages debugging your scripts, adding echos, prints and var_dumps everywhere? Then spent almost the same time cleaning up your debugging mess afterwards?
[hr]
You need an IDE.
Ok, so what it is?
An integrated development environment (IDE) or interactive development environment is a software application that provides comprehensive facilities to computer programmers for software development. An IDE normally consists of a source code editor, build automation tools and a debugger. Several modern IDEs integrate with Intelli-sense coding features.[hr]
Comparison of plain code
Let’s start with comparing some data as seen in notepad vs an IDE. Try to compare how easy it is to spot the error.
Notepad:
[code]class userController extends stdController {
public $params;
public $model;
public function __construct(userParams $params, userModel $model) {
parent::__construct($params, $model);
$this->params = $params;
$this->model = $model;
if (empty($this->params->getAction)) {
throw new \MissingParamsException('Action missing.');
}
switch ($this->params->getAction) {
case 'create':
$this->model->createUser($this->params);
break;
case 'items':
if (empty($this->params->getId) && empty($this->params->getName)) {
throw new \MissingParamsException('You need to supply a user ID or user name.);
}
$user = !empty($this->params->getId)
? $this->model->getUserById($this->params->getId)
: $this->model->getUserByDisplayName($this->params->getName);
break;
default:
throw new \InvalidActionException('Invalid action: ' . $this->params->getAction);
}
}
}[/code]
IDE:
[php]class userController extends stdController {
public $params;
public $model;
public function __construct(userParams $params, userModel $model) {
parent::__construct($params, $model);
$this->params = $params;
$this->model = $model;
if (empty($this->params->getAction)) {
throw new \MissingParamsException('Action missing.');
}
switch ($this->params->getAction) {
case 'create':
$this->model->createUser($this->params);
break;
case 'items':
if (empty($this->params->getId) && empty($this->params->getName)) {
throw new \MissingParamsException('You need to supply a user ID or user name.);
}
$user = !empty($this->params->getId)
? $this->model->getUserById($this->params->getId)
: $this->model->getUserByDisplayName($this->params->getName);
break;
default:
throw new \InvalidActionException('Invalid action: ' . $this->params->getAction);
}
}
}[/php]
The second one is a bit easier, right?
[hr]
But I use Notepad++ (or similar), and it has code formatting!
Ok, definitely better than plain old notepad, but you still fall a bit short.
[hr]
IDE’s offer code completion!
Not only do they hold the entire PHP library, but they will also index your project. This means that when you are typing in an IDE it will auto-suggest variable/object/function-names, in real time. It will even keep track of what’s in scope at that time so you don’t get suggested stuff you don’t have access to. On top of it all it will display info for the function/object, so you know what it does, which parameters it accepts, which exception(s) it throws, and what it returns.
[hr]
IDE’s tell you when you’ve messed up!
Errors and warnings will show up in the line-number column (warning and stop icons).
Examples:
If you initialize a variable, and don’t use it later in the code, the IDE will tell you.
[php]$a = ‘test’;[/php]
Variable $a seems to be unused in its scope
The same if you do the error we did earlier on
[php]if ($userId = 1) {[/php]
Accidental assignment in a condition $result = true
(Alt-Enter shows hints)
And simply pressing Alt-Enter brings up a selection of solution-suggestions (changing to == or ===), simple, effective, brilliant
[hr]
IDE’s hotlinks functions and classes!
This is an awesome feature, everywhere in your code you can ctrl-click function- or class-names and you will be taken directly to that function or class!
[hr]
IDE’s keep track of TODO’s!!
Ever seen things like this:[php]// @TODO: benchmark as I have a iffy feeling about this function[/php]
Most IDE’s have a panel where you can see all todo’s listed, and clicking them takes you directly to that place in the code.
[hr]
IDE’s give you the power of debugging!
Hands down the best feature ever and should be a part of every developers toolkit. Imagine beeing able to set a breakpoint in your editor, refresh the website and have the code break at the point you set. Then, in real time, you can step trough the code and visually see which line it runs.
This way you can without any doubt see how if/else/switches/etc are handled. You will be taken on a visual tour trough the code execution and will jump between functions and classes. All this while having a visual representation of all variables and their values. If I have time I will write a tutorial on how to set up a developer environment with debugging.
See a quick and simple demo of this here
[hr]
In addition you can
[ul][li]Version control with SVN/GIT/Mercurial[/li]
[li]SVN/GIT/Mercurial/Local diff[/li]
[li]Connect to and control SQL databases[/li]
[li]and much much more[/li][/ul]
[hr]
Since I am pro open source I will obviously recommend to find try out one of the free IDE’s on the market. I myself use Netbeans, which is great because it’s free, open source and works on Windows and Unix
I’m however in no way stating it is the best IDE out there, so try a few out and find one you like.
Great IDE’s: Netbeans | Sublime Text | PhpStorm | Zend Studio