LoginSignup
1
1

More than 1 year has passed since last update.

Windowsで開発したPHPをCentOSにデプロイしたときに起きやすいエラー

Last updated at Posted at 2022-06-17
../

Windowsで開発したPHPをCentOS Stream 8環境にデプロイしたときに、いくつかエラーやワーニングが出ていたのでメモしておく。軽微な内容です。

DBの接続先を切り替えておくこと

DBの接続情報が異なる場合、例えば、dbname,user,passwordが異なると、以下の感じのエラーになる。

PHP Fatal error:  Uncaught TypeError: Return value of common\\integration\\PersistenceBase::pdo() 
must be an instance of PDO, null returned in /common/integration/PersistenceBase.php:45

環境を判別して制御を切り替えるように記述しておくこと。

  public static function isWindows() : bool {
    return (DIRECTORY_SEPARATOR == '\\');
  }
  ...
  if (self::isWindows()){
  	...
  } else {
  	...
  }

パス表現でバックスラッシュ(\)を混在させないこと

パスを表現するとき、Windowsでは以下のようにバックスラッシュ(\)を使ってもエラーにならないが、

  require_once(__DIR__."/common\integration\CommonProps.php");

CentOSではエラーになる。

PHP Warning:  require_once(/common\\integration\\CommonProps.php): 
failed to open stream: No such file or directory in common/integration/HelloProps.php on line 3

スラッシュ(/)で統一しておこう。

  require_once(__DIR__."/common/integration/CommonProps.php");

※誤記があったので修正しました。(バッククォート --> バックスラッシュ)

$_SESSION['xxx']はisset()でチェックすること

Windowsでは、$_SESSION['xxx']で要素がない場合にもエラーにならなかったが、CentOSではエラーになる。

  const THIS_CONTEXT = 'context-users';
  $context = $_SESSION[self::THIS_CONTEXT];

の場合、以下のようなエラーが出る。

PHP Notice:  Undefined index: context-users in /users/presentation/UsersContext.php on line 42, 
referer: http://kankeri.com/users/

isset()のチェックを挟むのを忘れてはいけない。

  const THIS_CONTEXT = 'context-users';
  $context = null;
  if (isset($_SESSION[self::THIS_CONTEXT])){
    $context = $_SESSION[self::THIS_CONTEXT];
  }

※コメントをいただきましたが、上記はOS環境の違いではなく、PHPの設定(php.ini)の差異によるようです。error_reporting = E_ALLあたりの設定が影響している。

エラーログには日本語を含めないこと

エラーログに日本語などマルチバイトのコードを含めると、ログが読みにくい。エンコードを指定できるエディタなどで開けば問題ないのかもしれないが、moreやcatでは読みにくい。大量のログの中で目立たせるために「■」を使っていたが「@@@」に変更した。

  error_log('■ ' . $sql . ' --> ' . $cnt);

のような日本語の埋込みはやめた。

  const TRACE_PREFIX = '@@@ ';
  error_log(self::TRACE_PREFIX . $sql . ' --> ' . $cnt);
../
1
1
4

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
1
1