Visual Studio CodeでPHP開発環境を構築する手順

PHP

PHP開発環境構築の目的

  • 実装が想定通りに動いているか検証したい
  • システムの仕組みが知りたい
  • ボタンの実行SQLが知りたい
  • ソースステップ毎にデバグしたい
Demo GIF

PHP開発環境構築手順

XAMPPインストール

XAMPPはPHPの実行環境を提供される。OSに応じて、適切なバージョンをダウンロードし、インストールしてください。

XAMPP Installers and Downloads for Apache Friends
XAMPP is an easy to install Apache distribution containing MariaDB, PHP and Perl.

VSCodeエディタをインストール

Visual Studio Codeは軽量のコードエディタですが、プラグイン機能が多く、ほとんどの言語を対応されてある。

Visual Studio Code - Code Editing. Redefined
Visual Studio Code is a code editor redefined and optimized for building and debugging modern web and cloud applications.  Visual Studio Code is free and availa...

VSCode拡張機能「PHP IntelliSense」インストール

VSCode側の「基本設定(setting.json)」を編集する

基本設定「[Ctrl] + [Shift] + [P]→Open Setting(JSON)」を開く

設定ファイル(JSON)に下記を追記する

    "php.validate.executablePath": "C:\\xampp\\php\\php.exe",
    "php.validate.enable": false,
    "php.suggest.basic": false,
    "php.debug.executablePath": "C:\\xampp\\php\\php.exe",

VSCode拡張機能「PHP Debug」「PHP Extension Pack」をインストール

PHP Debug」と「PHP Extension Pack」をVSCodeにインストールする。

手順の詳細は下記のページをご参照ください

  • https://marketplace.visualstudio.com/items?itemName=xdebug.php-debug
  • https://code.visualstudio.com/docs/editor/debugging

PHP Debugを実行するためのDLLを取得

XAMPPのApacheサービスを起動して、下記のリンクを開きphpinfoの情報をCtrl+A、Ctrl+Cでコピーする。http://localhost/dashboard/phpinfo.php

Xdebugの『Installation Wizard』にコピーされた文字列を貼り付けて「Analyse my phpinfo() output」を押す。

次はDLLのダウンロードリンクが表示され、リンクをクリックしDLLを取得する

php.iniを編集してDLLを追加

「C:\xampp\php\ext」にダウンロードしたDLLをコピーする

C:\xampp\php\php.iniを下記のように追記する。

[XDebug]
zend_extension = "C:\xampp\php\ext\php_xdebug-3.1.4-7.4-vc15-x86_64.dll"
xdebug.mode = debug
xdebug.start_with_request = yes

xdebugのバージョン確認

http://localhost/dashboard/phpinfo.phpでxdebugのバージョンを確認する

C:\xampp\php\php.iniを下記のように追記するのが間違いない。php_xdebug-3.1.4-7.4-vc15-x86_64.dllの3.1.4がxdebudのバージョンとなっている。

xdebug.mode = debug
xdebug.start_with_request = yes

VSCodeでPHPのデバッグで「ステップ実行」してみる

VS codeにあるwordpressのindex.phpを開く、一番最小にブレークポイントを設定する

デバグモードを起動し、Listen for Xdebug状態になったら、ブラウザでindex.phpを開く

関連サイト

  • https://www.apachefriends.org/jp/index.html
  • https://code.visualstudio.com/
  • https://marketplace.visualstudio.com/items?itemName=xdebug.php-debug
  • https://code.visualstudio.com/docs/editor/debugging
  • https://xdebug.org/
  • https://xdebug.org/wizard

コメント

タイトルとURLをコピーしました