総数:19 今日:1 昨日:0
Setting up Visual Studio Code for php
参考url:
VisualStudioCodeのPHP
【PHP】VSCodeでPHP開発環境を整えてみよう(2020年夏)【おすすめ拡張機能】
PHP設定を変更するには、ユーザー設定またはワークスペース設定(Ctrl +,)を開き、「php」と入力して、使用可能な設定のリストをフィルタリングします。
PHPリンターを制御するための3つの設定があります。
| 設定項目 | 説明 | 設定手順 |
| php.validate.enable | PHPリンティングを有効にするかどうかを制御します。デフォルトで有効になっています。 | |
| php.validate.executablePath | ディスク上のPHP実行可能ファイルを指します。PHP実行可能ファイルがシステムパス上にない場合は、これを設定します。 | PHP実行可能パスを設定するには、[ PHP]> [検証:実行可能パス]の下の[ settings.jsonで編集]リンクを選択します。これにより、ユーザーファイルが開きます。PHPインストールへのパスを含む設定を追加します。 |
| php.validate.run | 検証が保存時(値:)"onSave"またはタイプ(値:)のどちらでトリガーされるかを制御します"onType"。デフォルトは保存時です。 |
/.config/Code/User/settings.json
{
"php.validate.executablePath": "/usr/bin/php"
}
Visual Studio Codeには、PHPの一般的なスニペットのセットが含まれています。
これらにアクセスするには、Ctrl + Spaceを押して、コンテキスト固有のリストを取得します。
VS Code Marketplaceには多くのPHP言語拡張機能があり、さらに多くのものが作成されています。
拡張機能ビュー(Ctrl + Shift + X)でVS Code内からPHP拡張機能を検索し、
「php」と入力して拡張機能のドロップダウンリストをフィルタリングできます。
PHPをインストールするときにほぼ必ず一緒に入れるデバッグツール
Xdebugのインストールが必須です。
VSCode上で再生ボタンや停止ボタン的なUIを操作しながら、1行ずつデバッグ実行ができるようになります。
コーディング全般をサポートするエクステンションです。
コードの入力補完、文法誤りチェック、マウスオーバー時のドキュメントの表示、親クラスなど違うファイル上のクラスの定義へのジャンプ……などが可能になります。
VSCode自体もPHPの基本的な補完は行ってくれるので、
バッティングするため設定の以下を変更します。
| PHP > Suggest: Basic | 組み込みの言語候補機能。 | Ctrl+, でSettings画面開き、Extensions->PHPで表示させて、チェックを外す(false) |
| PHP > Validate: Enable | 組み込みのPHP検証。 | Ctrl+, でSettings画面開き、Extensions->PHPで表示させて、チェックを外す(false) |
/.config/Code/User/settings.json
{
"php.validate.executablePath": "/usr/bin/php",
"php.suggest.basic": false,
"php.validate.enable": false
}
XDebugを使用したPHPデバッグは、PHP Debug extension 機能を介してサポートされます。
VSCodeで動作するようにXDebugを構成するためのPHP Debug extension 機能の指示に従います。
参考url:
PHP Debug Adapter for Visual Studio Code
Install the extension: Press F1, type ext install php-debug.
このextensionは、VS CodeとXdebug ( by Derick Rethans ) の間のdebug adapterです。
Xdebugは、サーバーにインストールする必要のあるPHP extension (.so ファイル)です。
installing xdebug on ubuntu desktop 20.04.2を参照
Configure PHP to use Xdebug by adding zend_extension=path/to/xdebug to your php.ini.
The path of your php.ini is shown in your phpinfo() output under "Loaded Configuration File".
installing xdebug on ubuntu desktop 20.04.2を参照
http://192.168.3.13/info.phpで表示されたphpのsettingsの項目は下記だった。
| Configuration File (php.ini) Path | /etc/php/7.4/apache2 |
| Loaded Configuration File | /etc/php/7.4/apache2/php.ini |
http://localhost/xdebug_info.php で表示されたphpのsettingsの項目は下記だった。
| Configuration File (php.ini) Path | /etc/php/7.4/cli |
| Loaded Configuration File | /etc/php/7.4/apache2/php.ini |
For Xdebug v3.x.x:
sudo gvim -f /etc/php/7.4/apache2/php.ini
xdebug.mode = debug xdebug.start_with_request = yes xdebug.client_port = 9000
下記のファイルも追記する。
sudo gvim -f /etc/php/7.4/cli/php.ini
Please note that the default Xdebug port changed between Xdebug v2 to v3 from 9000 to 9003.
The extension still defaults to 9000, so make sure your configuration in launch.json and php.ini match.
make sure your configuration in launch.json and php.ini match.
In your project, go to the debugger and hit the little gear icon and choose PHP.
A new launch configuration will be created for you with two configurations:
~/test_php/html/.vscode/launch.json
{
// 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
}
]
}
This setting will simply start listening on the specified port (by default 9000) for Xdebug.
If you configured Xdebug like recommended above,
everytime you make a request with a browser to your webserver or launch a CLI script Xdebug will connect and you can stop on breakpoints, exceptions etc.
This setting is an example of CLI debugging.
It will launch the currently opened script as a CLI,
show all stdout/stderr output in the debug console
and end the debug session once the script exits.
| setting item | description |
| request | Always "launch" |
| hostname | The address to bind to when listening for Xdebug (default: all IPv6 connections if available, else all IPv4 connections) |
| port | The port on which to listen for Xdebug (default: 9000) |
| stopOnEntry | Whether to break at the beginning of the script (default: false) |
| pathMappings | A list of server paths mapping to the local source paths on your machine, see "Remote Host Debugging" below |
| log | Whether to log all communication between VS Code and the adapter to the debug console. See Troubleshooting further down. |
| ignore | An optional array of glob patterns that errors should be ignored from (for example **/vendor/**/*.php) |
| xdebugSettings | Allows you to override Xdebug's remote debugging settings to fine tuning Xdebug to your needs. For example, you can play with max_children and max_depth to change the max number of array and object children that are retrieved and the max depth in structures like arrays and objects. This can speed up the debugger on slow machines. For a full list of feature names that can be set please refer to the Xdebug documentation.Xdebug documentation |
| feature name | description |
| max_children | max number of array or object children to initially retrieve |
| max_data | max amount of variable data to initially retrieve. |
| max_depth | maximum depth that the debugger engine may return when sending arrays, hashs or object structures to the IDE. |
| show_hidden | This feature can get set by the IDE if it wants to have more detailed internal information on properties (eg. private members of classes, etc.) Zero means that hidden members are not shown to the IDE. |
| program | Path to the script that should be launched |
| args | Arguments passed to the script |
| cwd | The current working directory to use when launching the script |
| runtimeExecutable | Path to the PHP binary used for launching the script. By default the one on the PATH. |
| runtimeArgs | Additional arguments to pass to the PHP binary |
| externalConsole | Launches the script in an external console window instead of the debug console (default: false) |
| env | Environment variables to pass to the script |
Line breakpoints
Conditional breakpoints
Function breakpoints
Step over, step in, step out
Break on entry
Breaking on uncaught exceptions and errors / warnings / notices
Multiple, parallel requests
Stack traces, scope variables, superglobals, user defined constants
Arrays & objects (including classname, private and static properties)
Debug console
Watches
Run as CLI
Run without debugging
[PHP] Xdebug のリモートデバッグ、理解していますか?