log > PHPのREPL、PsySHのメモ

PHPのREPL、PsySHの使い方メモ

公式サイト:PsySH(http://psysh.org/)

PHPのREPL(Read-Eval-Print Loop、対話的にプログラムを実行する環境)ライブラリ。ほかにもデバッグを補助する機能や、PHP Manualを簡易的に参照する機能がある。

インストール

$ composer g require psy/psysh:@stable
$ psysh
Psy Shell v0.1.11 (PHP 5.6.16 — cli) by Justin Hileman
>>> q
Exit:  Goodbye.

例ではglobal環境にインストールしたが、プロジェクトがあればプロジェクトごとにcomposer require psy/psysh:@stable --devした方がいい。

psyshでREPLが起動する。helpでコマンド一覧が見られる。

>>> help
  help      Show a list of commands. Type `help [foo]` for information about [fo
  ls        List local, instance or class variables, methods and constants.
  dump      Dump an object or primitive.
  doc       Read the documentation for an object, class, constant, method or pro
  show      Show the code for an object, class, constant, method or property.
  wtf       Show the backtrace of the most recent exception.
  trace     Show the current call stack.
  buffer    Show (or clear) the contents of the code input buffer.
  clear     Clear the Psy Shell screen.
  history   Show the Psy Shell history.
  exit      End the current session and return to caller

help単体だと、コマンド一覧の表示。helpの後にコマンドをつけることでコマンドの解説。ex:help ls,help wtf

exitで終了できる。(quit, qもAliasとして設定してある。)

使う

(コマンドなしで)設定済み変数を打ち込むと、変数の中身が表示される。

>>> $foo = "foo";
=> "foo"
>>> $foo
=> "foo"

lsコマンドは現在の変数を把握できる。

>>> ls
>>> ls -la

Variables:
  $_   null
>>> $foo = "foo"
=> "foo"
>>> ls
Variables: $foo
>>> ls -la

Variables:
  $foo   "foo"
  $_     "foo"

$_は直前に扱った変数、$_eは直前のエラーを表す。

>>> throw new Exception($_)
Exception with message 'foo'
>>> $_e
=> <Exception #0000000033f9114200000001170b7c6f> {
       message: "foo",
       file: "/Users/deprode/.composer/vendor/psy/psysh/src/Psy/ExecutionLoop/Loop.php(67) : eval()'d code",
       line: 1
   }

定義したクラスはprivate変数を含めて見ることができる。

>>> class Foo {
...     private $bar;
...     public function __construct($b) {
...        $this->bar = $b;
...     }
...     public function getBar() {
...        return $this->bar;
...     }
... }
>>> $foo = new Foo("foo");
=>  {}
>>> ls -la $foo

Class Properties:
  $bar   "foo"

Class Methods:
  __construct   public function __construct($b)
  getBar        public function getBar()

tips

  • ~/.config/psysh/config.phpに設定ファイルを設置できる
  • historyコマンドで今までのコマンドを確認することができる
  • wtfコマンドで直前の例外を表示できる
  • traceコマンドで現在のコールスタックを見ることができる
  • docコマンドでPHPDoc形式のコメントがあればオブジェクトを指定してドキュメントを表示できる
    • ほかにも、GithubからリンクされているPHP Manualを~/.local/share/psysh/または~/.psysh/に配置することで、docコマンドでマニュアルを参照できる。ex:doc print_r

デバッグ

Rubyのbinding.pry感覚でブレークポイントを設置できる(らしい)。1

eval(\Psy\sh());を差し込むことで、その地点の情報を見ることができる。(ただ、ステップ実行や実行後の動作保証はないので、そういうのをしたい場合はxdebug使う。)

<?php
require('vendor/autoload.php');

class Foo {
    public $bar;
    public function hello() {
        return 'hello' . $this->bar;
    }
}
$foo = new Foo();
$foo->bar = 'baz';
eval(\Psy\sh());
echo $foo->hello() . PHP_EOL;

showコマンドを使うと、その時点で把握しているオブジェクト(≠インスタンス)のコードを見ることができる。

$ php test.php
#!/usr/bin/env php

Psy Shell v0.6.1 (PHP 5.6.16 — cli) by Justin Hileman
>>> show $foo
  > 4| class Foo {
    5|     public $bar;
    6|     public function hello() {
    7|         return 'hello' . $bar;
    8|     }
    9| }

ちょっと触っただけなので、間違ってたらごめん。

参考