php.md

  1# PHP
  2
  3PHP support is available through the [PHP extension](https://github.com/zed-extensions/php).
  4
  5- Tree-sitter: [tree-sitter/tree-sitter-php](https://github.com/tree-sitter/tree-sitter-php)
  6- Language Server: [phpactor/phpactor](https://github.com/phpactor/phpactor)
  7- Alternate Language Server: [bmewburn/vscode-intelephense](https://github.com/bmewburn/vscode-intelephense/)
  8
  9## Install PHP
 10
 11The PHP extension requires PHP to be installed and available in your `PATH`:
 12
 13```sh
 14# macOS via Homebrew
 15brew install php
 16
 17# Debian/Ubuntu
 18sudo apt-get install php-cli
 19
 20# CentOS 8+/RHEL
 21sudo dnf install php-cli
 22
 23# Arch Linux
 24sudo pacman -S php
 25
 26# check PHP path
 27## macOS and Linux
 28which php
 29
 30## Windows
 31where php
 32```
 33
 34## Choosing a language server
 35
 36The PHP extension uses [LSP language servers](https://microsoft.github.io/language-server-protocol) with Phpactor as the default. If you want to use other language servers that support Zed (e.g. Intelephense or PHP Tools), make sure to follow the documentation on how to implement it.
 37
 38### Intelephense
 39
 40[Intelephense](https://intelephense.com/) is a [proprietary](https://github.com/bmewburn/vscode-intelephense/blob/master/LICENSE.txt#L29) language server for PHP operating under a freemium model. Certain features require purchase of a [premium license](https://intelephense.com/buy).
 41
 42To use Intelephense, add the following to your `settings.json`:
 43
 44```json [settings]
 45{
 46  "languages": {
 47    "PHP": {
 48      "language_servers": ["intelephense", "!phpactor", "!phptools", "..."]
 49    }
 50  }
 51}
 52```
 53
 54To use the premium features, you can place your license file inside your home directory at `~/intelephense/licence.txt` for macOS and Linux, or `%USERPROFILE%/intelephense/licence.txt` on Windows.
 55
 56Alternatively, you can pass the licence key or a path to a file containing the licence key as an initialization option. To do this, add the following to your `settings.json`:
 57
 58```json [settings]
 59{
 60  "lsp": {
 61    "intelephense": {
 62      "initialization_options": {
 63        "licenceKey": "/path/to/licence.txt"
 64      }
 65    }
 66  }
 67}
 68```
 69
 70### PHP Tools
 71
 72[PHP Tools](https://www.devsense.com/) is a proprietary language server that offers free and premium features. You need to [purchase a license](https://www.devsense.com/en/purchase) to activate the premium features.
 73
 74To use PHP Tools, add the following to your `settings.json`:
 75
 76```json [settings]
 77{
 78  "languages": {
 79    "PHP": {
 80      "language_servers": ["phptools", "!intelephense", "!phpactor", "..."]
 81    }
 82  }
 83}
 84```
 85
 86To use the premium features, you can add your license in `initialization_options` in your `settings.json`:
 87
 88```json [settings]
 89{
 90  "lsp": {
 91    "phptools": {
 92      "initialization_options": {
 93        "0": "your_license_key"
 94      }
 95    }
 96  }
 97}
 98```
 99
100or, set environment variable `DEVSENSE_PHP_LS_LICENSE` on `.env` file in your project.
101
102```env
103DEVSENSE_PHP_LS_LICENSE="your_license_key"
104```
105
106Check out the documentation of [PHP Tools for Zed](https://docs.devsense.com/other/zed/) for more details.
107
108### Phpactor
109
110To use Phpactor instead of Intelephense or any other tools, add the following to your `settings.json`:
111
112```json [settings]
113{
114  "languages": {
115    "PHP": {
116      "language_servers": ["phpactor", "!intelephense", "!phptools", "..."]
117    }
118  }
119}
120```
121
122## PHPDoc
123
124Zed supports syntax highlighting for PHPDoc comments.
125
126- Tree-sitter: [claytonrcarter/tree-sitter-phpdoc](https://github.com/claytonrcarter/tree-sitter-phpdoc)
127
128## Debugging
129
130The PHP extension provides a debug adapter for PHP via Xdebug. There are several ways to use it:
131
132```json
133[
134  {
135    "label": "PHP: Listen to Xdebug",
136    "adapter": "Xdebug",
137    "request": "launch",
138    "port": 9003
139  },
140  {
141    "label": "PHP: Debug this test",
142    "adapter": "Xdebug",
143    "request": "launch",
144    "program": "vendor/bin/phpunit",
145    "args": ["--filter", "$ZED_SYMBOL"]
146  }
147]
148```
149
150These are common troubleshooting tips, in case you run into issues:
151
152- Ensure that you have Xdebug installed for the version of PHP you’re running.
153- Ensure that Xdebug is configured to run in `debug` mode.
154- Ensure that Xdebug is actually starting a debugging session.
155- Ensure that the host and port matches between Xdebug and Zed.
156- Look at the diagnostics log by using the `xdebug_info()` function in the page you’re trying to debug.