LoginSignup
41
48

More than 3 years have passed since last update.

IntelliJ IDEA PHP7の開発環境を作成する

Last updated at Posted at 2016-04-01

概要

IntelliJ IDEAのPHP Pluginを使って開発環境を整える手順。

実行環境

macOS Mojave バージョン 10.14.5
IntelliJ IDEA Version 2019.1.3

前提条件として、IntelliJ IDEAのインストールと日本語化でインストールを行っている事。

PHP Pluginのインストール

プラグインの検索フォームに"PHP"と入力しPHP Pluginをインストールする。

phpplugin.png

「Install」をクリックするとインストールが開始される。
※インストール完了後に再起動を求められるので再起動を行う。

MacにPHP7をインストールする

phpenvやphpbrewを使ったほうがPHP自体のバージョン管理が出来て良いと思うが今回は手早く環境を作りたかったので、バイナリパッケージをインストールした。
※インストールは以下のコマンドを実行するだけで完了。

bash
curl -s http://php-osx.liip.ch/install.sh | bash -s 7.0

インストール先は/usr/local/php5/bin/phpとなる。
以下のコマンドでバージョンの確認を行う。

bash
/usr/local/php5/bin/php -v

以下のように表示されたらインストールが正常に終了している。

bash
PHP 7.0.4 (cli) (built: Mar 10 2016 14:34:46) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
 with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
 with Xdebug v2.4.0RC3, Copyright (c) 2002-2015, by Derick Rethans

インタプリタの設定

Preferences → Languages & Frameworks → PHPからインタプリタの設定を行う。

phpsettings1.png

PHP Language levelを7に設定し先ほどインストールした、/usr/local/php5/bin/phpを設定する。

phpsettings2.png

まとめ

以上の手順でIntelliJ IDEA上でPHPの開発が可能になる。
思ったよりも簡単に環境構築が出来た。

参考記事

2017-02-18 追記

PHPのバージョンを7.1に上げましたがこの資料の通りで特にトラブルは発生しませんでした。

私の環境では既にPHP7.0系がインストールされていましたが、下記のコマンドを実行する事で /usr/local/php5/bin/php のパスがPHP7.1に変更されました。

7.1のインストール
curl -s https://php-osx.liip.ch/install.sh | bash -s 7.1
結果確認
/usr/local/php5/bin/php -v

PHP 7.1.1 (cli) (built: Feb 13 2017 10:05:49) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.1.1, Copyright (c) 1999-2017, by Zend Technologies
    with Xdebug v2.5.0, Copyright (c) 2002-2016, by Derick Rethans

2018-01-27 追記

PHPのバージョンを7.2に上げましたがこの資料の通りで特にトラブルは発生しませんでした。

しかし curl -s https://php-osx.liip.ch/install.sh | bash -s 7.2 を実行した際に「macOS High Sierraに正式対応していません。」みたいな感じの警告が出ました。

しかし今のところはトラブルは発生していませんし問題なく使えている状況です。

結果確認
/usr/local/php5/bin/php -v
PHP 7.2.0 (cli) (built: Dec 15 2017 18:10:15) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.2.0, Copyright (c) 1999-2017, by Zend Technologies

2018-10-02 追記

HomebrewでPHPをインストールするのも楽なのでアリです。

こちらの方法のメリットは最初から開発に必要なパッケージがある程度揃っている事です。

brew search php@7.2 を実行してみます。

Warning: Error searching on GitHub: GitHub {
  "message": "Bad credentials",
  "documentation_url": "https://developer.github.com/v3"
}

The GitHub credentials in the macOS keychain may be invalid.
Clear them with:
  printf "protocol=https\nhost=github.com\n" | git credential-osxkeychain erase
Or create a personal access token:
  https://github.com/settings/tokens/new?scopes=gist,public_repo&description=Homebrew
and then set the token as: export HOMEBREW_GITHUB_API_TOKEN="your_new_token"

のような警告が出る場合は警告メッセージに表示されている通り、GitHubにアクセストークンを登録しましょう。

export HOMEBREW_GITHUB_API_TOKEN="your_new_token" とあるように環境変数に取得したアクセストークンを設定します。

警告が消えたら brew install php@7.2 を実行します。

一度ターミナルを再起動して php -v を実行すると以下のようにバージョンが切り替わっている事を確認出来ます。

PHP 7.2.10 (cli) (built: Sep 14 2018 07:07:08) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.10, Copyright (c) 1999-2018, by Zend Technologies

which php の結果も /usr/local/bin/php になっており切り替わっている事が確認出来ます。

事実上、開発に必須になっている composer もインストールしておきましょう。

brew install composer を実行すれば問題ありません。

さらにローカル開発時はデバッグを行いたいのでxdebugも導入します。

pecl install xdebug を実行しましょう。

正常終了すると以下のようなログが出ます。

Build process completed successfully
Installing '/usr/local/Cellar/php/7.2.10/pecl/20170718/xdebug.so'
install ok: channel://pecl.php.net/xdebug-2.6.1
Extension xdebug enabled in php.ini

php.ini を確認すると zend_extension="xdebug.so" が先頭に書き込まれている事が確認出来ます。

ちなみにphp.ini の場所は php --ini で確認出来ます。

Configuration File (php.ini) Path: /usr/local/etc/php/7.2
Loaded Configuration File:         /usr/local/etc/php/7.2/php.ini
Scan for additional .ini files in: /usr/local/etc/php/7.2/conf.d
Additional .ini files parsed:      /usr/local/etc/php/7.2/conf.d/ext-opcache.ini

再び php -v を実行すると Xdebug が読み込まれている事を確認出来るかと思います。

PHP 7.2.10 (cli) (built: Sep 14 2018 07:07:08) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Xdebug v2.6.1, Copyright (c) 2002-2018, by Derick Rethans
    with Zend OPcache v7.2.10, Copyright (c) 1999-2018, by Zend Technologies

2019-07-22 追記

Homebrewで brew install php@7.3 を実行した手順も上手くいきました。

またMacOS Mojave、IntelliJ IDEA Version 2019でも正常動作したので、実行環境を書き換えました。

参考記事

41
48
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
41
48