[PHP-users 33815] exceptionで日本語を通す方法

kameoyaji kenjik @ trialgoods.com
2008年 7月 11日 (金) 20:28:00 JST


自前でExceptionを作成した場合のメッセージを日本語で通す方法は無いのでしょうか?

下記に書いたテストプログラムで、exceptionを自前で作っています、
その時のメッセージを日本語でセットしています。(2)の場所。

自分の期待値としては、(3)で表示している場所で、日本語のメッセージが出ることを期待していたのですが、
日本語が正しく表示されません。

(2)のメッセージを英語にすると、正しく表示されるのですが、MLのサンプルや、
ネット上サンプルでは、日本語をセットしているものが多いので、なにがしらの
方法で日本語画表示出来る方法が有るのかと思いメールさせて頂きました。

日本語が通らなかったからといって大きな問題ではないのですが、日本語のメッ
セージにした方が分かりやすいもので。


テストプログラム(exceptest.php)
<?

try {
	$et = new exceptest();
	$et->SetArray('a' , 0 );
	$et->SetArray('c' , 0 );  // Exceptionが発生する (1)
} catch (Exception $e) {
	var_dump($e);	// (4)
	echo 'execute failed: ' . $e->getMessage() . "\n";   // <= ここで'配列にキーが無い'のメッセージが出る予定(3)
	echo '    error code: ' . $e->getCode() . "\n";
	echo 'Stack Trace   :\n' .  $e->getTraceAsString() . "\n";
}



class exceptest{
	public $_testarry = array('a' => NULL, 'b' => NULL);

	function SetArray($Name, $Value ){
		if (array_key_exists($Name, $this->_testarry)) {
			$this->_testarry[$Name] = $Value;
		} else {
			throw new Exception('配列にキーが無い');	// (2)
		}
	}
}

?>

(4)で取ったvar_dumpの結果は下記になります。
(一部ファイルパス等を書き直しています)

object(Exception)#2 (6) {
  ["message:protected"]=>
  string(16) "zL["
  ["string:private"]=>
  string(0) ""
  ["code:protected"]=>
  int(0)
  ["file:protected"]=>
  string(37) "/???????/exceptest.php"
  ["line:protected"]=>
  int(23)
  ["trace:private"]=>
  array(1) {
    [0]=>
    array(6) {
      ["file"]=>
      string(37) "/?????????/exceptest.php"
      ["line"]=>
      int(6)
      ["function"]=>
      string(8) "SetArray"
      ["class"]=>
      string(9) "exceptest"
      ["type"]=>
      string(2) "->"
      ["args"]=>
      array(2) {
        [0]=>
        string(1) "c"
        [1]=>
        int(0)
      }
    }
  }
}

環境は

CENTOS 5.2
PHP(CLI版でテストしています)のVer、Configureは下記になります。
(yumでインストールした状態です)

phpinfo()
PHP Version => 5.1.6

System => Linux ??????.com 2.6.18-8.el5 #1 SMP Thu Mar 15 19:57:35 EDT 2007 i686
Build Date => Mar 14 2007 18:34:22
Configure Command =>  './configure' '--build=i686-redhat-linux-gnu' '--host=i686-redhat-linux-gnu' '--target=i386-redhat-linux-gnu' '--program-prefix=' '--prefix=/usr' '--exec-prefix=/usr' '--bindir=/usr/bin' '--sbindir=/usr/sbin' '--sysconfdir=/etc' '--datadir=/usr/share' '--includedir=/usr/include' '--libdir=/usr/lib' '--libexecdir=/usr/libexec' '--localstatedir=/var' '--sharedstatedir=/usr/com' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--cache-file=../config.cache' '--with-libdir=lib' '--with-config-file-path=/etc' '--with-config-file-scan-dir=/etc/php.d' '--disable-debug' '--with-pic' '--disable-rpath' '--without-pear' '--with-bz2' '--with-curl' '--with-exec-dir=/usr/bin' '--with-freetype-dir=/usr' '--with-png-dir=/usr' '--enable-gd-native-ttf' '--without-gdbm' '--with-gettext' '--with-gmp' '--with-iconv' '--with-jpeg-dir=/usr' '--with-openssl' '--with-png' '--with-pspell' '--with-expat-dir=/usr' '--with-pcre-regex=/usr' '--with-zlib' '--with-layout=GNU' '--enable-exif' '--enable-ftp' '--enable-magic-quotes' '--enable-sockets' '--enable-sysvsem' '--enable-sysvshm' '--enable-sysvmsg' '--enable-track-vars' '--enable-trans-sid' '--enable-yp' '--enable-wddx' '--with-kerberos' '--enable-ucd-snmp-hack' '--with-unixODBC=shared,/usr' '--enable-memory-limit' '--enable-shmop' '--enable-calendar' '--enable-dbx' '--enable-dio' '--with-mime-magic=/etc/httpd/conf/magic' '--without-sqlite' '--with-libxml-dir=/usr' '--with-xml' '--enable-force-cgi-redirect' '--enable-pcntl' '--with-imap=shared' '--with-imap-ssl' '--enable-mbstring=shared' '--enable-mbstr-enc-trans' '--enable-mbregex' '--with-ncurses=shared' '--with-gd=shared' '--enable-bcmath=shared' '--enable-dba=shared' '--with-db4=/usr' '--with-xmlrpc=shared' '--with-ldap=shared' '--with-mysql=shared,/usr' '--with-mysqli=shared,/usr/bin/mysql_config' '--enable-dom=shared' '--with-dom-xslt=/usr' '--with-dom-exslt=/usr' '--with-pgsql=shared' '--with-snmp=shared,/usr' '--enable-soap=shared' '--with-xsl=shared,/usr' '--enable-xmlreader=shared' '--enable-xmlwriter=shared' '--enable-fastcgi' '--enable-pdo=shared' '--with-pdo-odbc=shared,unixODBC,/usr' '--with-pdo-mysql=shared,/usr' '--with-pdo-pgsql=shared,/usr' '--with-pdo-sqlite=shared,/usr'
Server API => Command Line Interface
Virtual Directory Support => disabled
Configuration File (php.ini) Path => /etc/php.ini
Scan this dir for additional .ini files => /etc/php.d
additional .ini files parsed => /etc/php.d/ldap.ini





-- 
かめおやじと申します、お見知りおきを
個人で運営中のサイト
http://trialgoods.com/emoji 絵文字を使いこなして見るページ
http://katte.mixsn.com       勝手にシリーズ(ことば遊びサイト)
http://mobile.mixed-in.com 携帯向けの絵文字の情報サイト
http://15qm.com              15分間のメルアドサービス
                  (アングラなサイトですが 笑)
kameoyaji <kenjik @ trialgoods.com>



PHP-users メーリングリストの案内