phpのコマンドラインオプション

mb_convert_kanaの中身を見てみるd:id:maru_cc:20080212:1202831190で、結局コマンドラインで実行時のphp.iniの設定ということで、別に作成したiniファイルを読み込ませるという手段を書いたが、せっかくなので他のオプションも触ってみた。


詳しくはヘルプ参照のこと
http://jp.php.net/manual/ja/features.commandline.php

Apachehttpd.confや.htaccessレベルでphpの設定をした場合、php.iniの設定を変更しないといけない場合がある。
しかし、php.iniを変更できない場合、コマンドラインとして実行した場合に問題が出る。
そこで、iniファイルを別に用意し、コマンドライン実行時に個別に指定するという方法がある。

$ /path/to/php -c /path/to/cli_php.ini /path/to/script.php

しかし、これだと、php.iniを丸ごと指定しないといけなくなる。
特定の設定のみファイルから読み込むということが出来ない。


設定変更する箇所が少ない場合には、個別に指定することで回避することが出来る。
例として、mbstring.internal_encodingのみ上書きする場合。

$ /path/to/php -d mbstring.internal_encoding=UTF-8 /path/to/script.php

複数のオプションを上書きする場合には、-dオプションを列挙することで指定できる。

$ /path/to/php -d mbstring.internal_encoding=UTF-8 -d mbstring.detect_order=auto /path/to/script.php

特定の設定のみファイルで指定して上書きということが出来ないのだろうか?


他にもいくつかオプションがあるが、新しく知って便利だと思ったもので、関数・クラス・拡張モジュールについての情報 ・張モジュールについての設定情報 を列挙できるオプションがある。

$ php --rf [ファンクション名]
$ php --rc [クラス名]
$ php --re [拡張モジュール名]
$ php --ri [拡張モジュール名]

それぞれの結果、まずは --rf。

$ php --rf var_dump
Function [ <internal:standard> function var_dump ] {

  - Parameters [2] {
    Parameter #0 [ <required> $var ]
    Parameter #1 [ <optional> $... ]
  }
}
$ php --rf mb_convert_kana
Function [ <internal:mbstring> function mb_convert_kana ] {
}

サンプルのvar_dumpは細かく出るが、mb_convert_kanaなどはパラメータまで出ない。


関数のようにだが、内部的に言語構造や言語構成要素には使えない。

$ php --rf echo
Exception: Function echo() does not exist
$ php --rf list
Exception: Function list() does not exist
$ php --rf array
Exception: Function array() does not exist

http://jp.php.net/manual/ja/function.echo.php

echo() は実際には関数ではありません (言語構造です)。このため、使用する際に括弧は必要ありません。

http://jp.php.net/manual/ja/function.list.php

array() と同様に、 この関数は実際には関数ではなく言語の構成要素です。

http://jp.php.net/manual/ja/function.array.php

リテラル配列を表現するための 言語構成要素であり、通常の関数ではありません。


次に --rc

$ php --rc Directory
Class [ <internal:standard> class Directory ] {

  - Constants [0] {
  }

  - Static properties [0] {
  }

  - Static methods [0] {
  }

  - Properties [0] {
  }

  - Methods [3] {
    Method [ <internal:standard> public method close ] {
    }

    Method [ <internal:standard> public method rewind ] {
    }

    Method [ <internal:standard> public method read ] {
    }
  }
}

他にどんな組み込みクラスがあるのだろうか?

$ php --re mbstring
Extension [ <persistent> extension #20 mbstring version <no_version> ] {

  - INI {
    Entry [ mbstring.language <PERDIR,SYSTEM> ]
      Current = 'Japanese'
    }
    Entry [ mbstring.detect_order <ALL> ]
      Current = 'auto'
    }
(長いので略)
$ php --ri mbstring

mbstring

Multibyte Support => enabled
Multibyte string engine => libmbfl
HTTP input encoding translation => enabled
Multibyte (japanese) regex support => enabled
Multibyte regex (oniguruma) version => 4.4.4
Multibyte regex (oniguruma) backtrack check => On

mbstring extension makes use of "streamable kanji code filter and converter", which is distributed under the GNU Lesser General Public License version 2.1.

Directive => Local Value => Master Value
mbstring.language => Japanese => Japanese
mbstring.detect_order => auto => auto
mbstring.http_input => auto => auto
mbstring.http_output => pass => pass
mbstring.internal_encoding => EUC-JP => EUC-JP
mbstring.script_encoding => no value => no value
mbstring.substitute_character => no value => no value
mbstring.func_overload => 0 => 0
mbstring.encoding_translation => On => On
mbstring.strict_detection => Off => Off

この設定値を列挙するのは使えると思った。


他に、-aのInteractive modeの使い方がいまいちわからない。