facebook

Getting Started with PHP Development in Eclipse

This guide walks you through getting started with PHP development in Eclipse using CodeMix—from installing the prerequisites on your system, to configuring CodeMix for an optimal experience. Please use the table of contents in the right margin of the guide to jump to the section you need help with, or you may want to read the entire guide if you’re new to PHP development.

The CodeMix plugin is compatible with Eclipse-based IDEs and tools—like MyEclipse, Spring Tools Suite, and JBoss Tools—so you can continue working in the environment you are accustomed to.

An Introduction to CodeMix and PHP Development

CodeMix is an Eclipse plugin that gives you access, directly from Eclipse, to a wide array of technologies from VS Code, and from add-on extensions built for Code OSS. When talking specifically about PHP support, CodeMix gets fundamental PHP support from Code OSS, and additional functionality from an array of PHP specific extensions, which are all part of our PHP extension pack – see the table below for details.

FunctionalitySource
Syntax highlighting, Validation, SnippetsCode OSS (VS Code core)
Content AssistNavigation, Hover Support, Additional ValidationPHP Intellisense Extension
DebuggingPHP Debug Extension
Code formattingphpfmt – PHP formatter Extension
OutlinePHP Symbols Extension

Note: The PHP extension pack also contains several additional extensions which add more general functionality, like auto closing of HTML tags, JavaScript snippets, and formatters for CSS, JS and HTML.

Installing CodeMix and the PHP Extension Pack

If you don’t already have CodeMix installed, please use this marketplace entry to install CodeMix into Eclipse (Neon or higher), MyEclipse 2017 or Angular IDE. On restart after installation, the PHP Extension Pack is automatically installed. See this document for additional information.

If you already have CodeMix installed, go to Help>CodeMix Extensions>Browse for Extension, and install the PHP Pack from the extension browser.

Please see this document for more on Extensions in CodeMix.

Configuring CodeMix for PHP Development

If you do not have PHP installed on your system, please see Installing PHP.

Before getting started with PHP coding, configure your PHP preference pages for CodeMix.  

Note: Be sure to set to the path of the PHP executable on your system


PHP preference pages

Working with PHP


Projects

To create a new PHP project, use the wizard at File>New>Project>CodeMix>PHP Project. A simple Hello World project is created in your workspace.

To import an existing project, use File>Open Projects from File System and point to the folder containing your project.

PHP Source

To create a new PHP file, use the wizard at File>New>Other>CodeMix>PHP File. You can simply create a regular file with a php extension as well.

When editing a PHP file, you will get content assist and validation. You can even use snippets to insert commonly used bits of code by selecting a snippet from the content assist list. Press `Ctrl/Cmd` when hovering over variables and functions to see their definitions and implementations, you can always click to navigate to those locations too.

PHP IntelliSense

PHP Validation

The Command Palette

Press `Ctrl/Cmd+Shift+P` to open the command palette. You can use the Insert Snippets command to insert a snippet, and others like the Format Document command to format the current file. There are dozens of other commands that you may find useful, explore the palette to find more.

Press `Ctrl+O` to bring up the local symbol search allowing you to jump to different PHP elements in the current file.

Use `Ctrl+P` to open any file in your workspace efficiently; you can even type in # to change this to a workspace-wide symbol search and search for symbols instead of filenames.

Watch the video for more coding tips and tricks.

Debugging

Whether you’re debugging a web application or a utility you’ve written, with the PHP Debug extension and Xdebug, you get the same robust and capable debugging experience that you’ve come to expect from the Eclipse debugger.

Installing Xdebug
Ensure you’ve installed and configured Xdebug prior to debugging a server side application or a CLI. Several PHP installations will include it, so you might just want to take a look at phpinfo() output to see if it’s already installed and enabled, along with a couple of key settings – look for the following in the output.

xdebug.remote_enable = On
xdebug.remote_autostart = On
xdebug.remote_port = 9000

Note: If the port value is not 9000, please remember the value for later.

If Xdebug is not installed, please follow this guide or this handy wizard which will give you the exact steps to take, if you feed it the output from php -i. You will also need to turn on remote debugging by adding the following to your php.ini:

[XDebug]
xdebug.remote_enable = 1
xdebug.remote_autostart = 1

Initiating a Debugging Session
  1. Bring up the context menu on your project and select Debug As>CodeMix Launch.
  2. This will create a launch.json file in the .vscode folder in the root of your project.
  3. Replace the entire file with the following, to create two debug configurations. Remember to update the port if the xdebug.remote_port value is different from the default, 9000.
    {
    	// Use IntelliSense to learn about possible attributes.
    	// Hover to view descriptions of existing attributes.
    	// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    	"version": "0.2.0",
    	"configurations": [
        	{
            	"name": "Listen for XDebug",
            	"type": "php",
            	"request": "launch",
            	"port": 9000
        	},
        	{
            	"name": "Launch currently open script",
            	"type": "php",
            	"request": "launch",
            	"program": "${file}",
            	"cwd": "${fileDirname}",
            	"port": 9000
        	}
    	]
    }
  4. Open the Debug Configurations dialog.

  5. Expand the CodeMix node and select the one of the following:
    Listen for XDebug (projectname)—to debug a server side PHP application.
    Launch currently open script (projectname)—to debug a console based PHP application. The file to debug must be open and focused in Eclipse.

Remember to place breakpoints in your code, they’ll be hit by the debugger when execution reaches this point. If you need to make advanced debugger settings, like breaking when a script is loaded, or specifying server-to-local-path mappings, please see the PHP Debug extension documentation for additional properties that can be set in launch.json.



For more on CodeMix’s debugging capabilities, please read our debugging documentation.

Reporting Issues

We maintain a public issue tracker for CodeMix, where you can find a list of issues already being handled, and their current status. Feel free to vote-up issues that you are affected by.

While there are certainly some exceptions, if you run into issues, like code being badly formatted, missing content assist suggestions, or simply bad validation, these problems are most likely caused by bugs in the core PHP support, or in the extensions, not in CodeMix. Regardless of this fact, do report these issues to us on our forums, and we’ll investigate them; realize that fixing some of these problems may be the responsibility of the corresponding extension developer.

Appendix A: Installing PHP

While PHP binaries are not required for basic PHP editing, you will need them for validation, linting and debugging; we recommend you get this taken care of at the start of your PHP journey. The tooling requires PHP 7 or higher, so if you have an earlier version, you will need to update it or install a separate version.

Now, you could just install PHP binaries and get going, but you may soon need a web server and database for local development and testing. Follow the next section to install such a package, or jump ahead if you only want to install PHP.

Installing a PHP Dev Package

If you’d like to install a complete dev suite, one option is XAMPP, which is an Apache distribution that includes, PHP, MariaDB (OSS fork of MySQL), and even Perl. It’s available for Windows, macOS and Linux here: https://www.apachefriends.org/download.htm.

Another option is MAMP, which you can get here: https://www.mamp.info/en/downloads/ MAMP is not available for Linux though.

Installing only PHP

Windows

https://windows.php.net/download has PHP archives that you can simply unzip into an accessible location.

macOS

If you’re on macOS Sierra or higher, you probably don’t need to do anything as it should include PHP 7. For earlier versions, you easily install it via Homebrew with the following command:
`brew install php71`

If you wish to check the version of PHP you have installed, run the following command:
`php -v`

If you need more assistance, these articles cover installing and updating PHP in detail.

Linux

Chances are that you already have php installed. Simply type `php -v` into a terminal to ensure you have PHP version 7 or higher. If you need to install PHP, you can use the following commands based on your distribution, though it is possible that additional prerequisites exist—given the variety of Linux distributions, we will not be covering those here:

DistributionCommand
Ubuntu / Mint / Debian`sudo apt-get install php`
CentOS`sudo yum install php`
Fedora`sudo dnf install php`
 
PHP Binary Path

After you’ve installed PHP, locate the PHP executable—you will need the path to this to configure the PHP support in CodeMix.

InstallationLikely Path to Binary
Windows binaries`{installation directory}/php.exe`
Linux and macOS`/usr/bin/php`
XAMPP`{installation directory}/php/php[.exe]`
MAMP`{installation directory}/bin/php/php{version}/bin/php[.exe]`